This is a syntax question related this answer from crono: https://stackoverflow.com/a/40869/1783806
@echo off
for /f " delims==" %%i in (counter.txt) do set /A temp_counter= %%i+1
echo %temp_counter% > counter.txt
assuming the count.bat
and counter.txt
are located in the same directory.
I get a "missing operator" error when I change the directory structure.
This is what CMD returns when I changed the structure to "C:\foo\counter.txt"
:
C:\foo>for /F " delims==" %i in ("C:\foo\counter.txt") do set /A temp_counter=%i+1
C:\foo>set /A temp_counter=C:\foo\counter.txt+1
Missing operator.
C:\foo>echo 0 1>"C:\foo\counter.txt"
How do I get the same code to work if the files are in different directories?
Can someone also explain why the error is occurring?
In the first code you do not quote the IN() clause, so it is treated as a file.
In the second code you do quote the IN() clause, so it is treated as a string literal instead of a file. If you want to use quotes around the file name then you must add the USEBACKQ
option to your FOR /F options. Type HELP FOR
for more info on the many variations of the FOR command.