json - How do I increment a filename in batch? -
i programming batch file can track lost android phones using google maps geolocation api. cannot understand wrong code. followed found on internet if run code, returns "the syntax incorrect" or that.it increments filename if basename exists. can show me wrong code , how correct it?
:mainprocessnew cd c:\users\%username%\documents %%g in (*.json) ( set file = %%g ) set "basename=data" set "n=0" :loop set /a n+=1 if exist "%basename%%n%.json ( goto loop ) echo.>"c:\users\%username%\documents\data%n%.json"
use batch code:
:mainprocessnew cd /d "%userprofile%\documents" %%g in (*.json) set "filename=%%g" set "basename=data" set "filenumber=0" :filenameloop set /a filenumber+=1 if exist "%basename%%filenumber%.json" goto filenameloop echo.>"%userprofile%\documents\%basename%%filenumber%.json"
the main mistake missing double quote in if condition line.
but there other mistakes , code lines improved.
don't use c:\users\%username%
because profile directory of user can on drive drive c:. use instead value of predefined environment variable userprofile
.
the path documents folder of current user can contain 1 or more spaces, example when user name contains space. therefore enclose user profile related folder paths in double quotes.
the command cd should used /d
change current drive if not 100% guaranteed current directory of batch process , new current directory on same drive.
never assign value environment variable spaces around equal sign, see why no string output 'echo %var%' after using 'set var = text' on command line? explanation of common mistake.
don't define command block parentheses commands for , if if 1 command should executed 2 commands designed running 1 command.
it advisable use variable names in camelcase notation. hard search n
find occurrences of environment variable, easy search filenumber
.
for understanding used commands , how work, open command prompt window, execute there following commands, , read entirely pages displayed each command carefully.
cd /?
echo /?
for /?
goto /?
if /?
set /?
Comments
Post a Comment