3

I am programming a batch file that can track lost android phones using the google maps geolocation API. I cannot understand what is wrong with my code. I followed everything I found on the internet but if I run the code, it returns "the syntax was incorrect" or something like that.It basically increments a filename if the basename exists. Can you show me what is wrong with my code and how do I correct it?

:MainProcessNew
cd C:\Users\%USERNAME%\Documents
for %%G IN (*.json) do (
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"
Mofi
  • 46,139
  • 17
  • 80
  • 143
Jezreel Martin
  • 89
  • 1
  • 12

2 Answers2

5

Use this batch code:

:MainProcessNew
cd /D "%USERPROFILE%\Documents"
for %%G in (*.json) do 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 is the missing double quote in IF condition line.

But there are also other mistakes and code lines which could be improved.

Don't use C:\Users\%USERNAME% because the profile directory of a user can be also on another drive than drive C:. Use instead the value of the predefined environment variable USERPROFILE.

The path to documents folder of the current user can contain 1 or more spaces, for example when the user name contains a space. Therefore always enclose user profile related folder paths in double quotes.

The command CD should be used always with /D to change also the current drive if it is not 100% guaranteed that current directory of batch process and new current directory are on same drive.

Never assign a value to an environment variable with spaces around equal sign, see Why is no string output with 'echo %var%' after using 'set var = text' on command line? for an explanation of this common mistake.

Don't define a command block with parentheses for the commands FOR and IF if just 1 command should be executed as those two commands are originally designed for running only 1 command.

It is advisable to use good variable names in CamelCase notation. It is hard to search for n to find all occurrences of that environment variable, but is very easy to search for FileNumber.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • cd /?
  • echo /?
  • for /?
  • goto /?
  • if /?
  • set /?
Mofi
  • 46,139
  • 17
  • 80
  • 143
0

may be one more n+1 will help?

:loop
set /a n+=1
if exist "%baseName%%n%.json" (
    goto loop
)
set /a n+=1
echo.>"C:\Users\%USERNAME%\Documents\data%n%.json"`
npocmaka
  • 55,367
  • 18
  • 148
  • 187
  • it still returns the same string. The syntax was incorrect. Maybe something wrong with the for loop? Sorry I am a total noob in for loops. – Jezreel Martin Aug 29 '16 at 08:25
  • 1
    @JezreelMartin - there is/was missing quote in the if condition.Can you check it now? – npocmaka Aug 29 '16 at 08:28
  • 1
    @npocmaka The additional `n+=1` is counterproductive. On first run with file `data1.json` not existing, the batch code produces `data2.json`. On second run `data1.json` still does not exist and therefore existing `data2.json` is overwritten. And this occurs on every run now. – Mofi Aug 29 '16 at 08:55