I have a file file.txt
that looks like this:
string1a,string1b
string2a,string2b
string3a,string3b
I am trying to run the following batch file:
@echo off
For /F "usebackq tokens=1,2 delims=," %%a in ("file.txt") do (
echo The output is:
echo %%a
echo %%b
set var_1=%%a
echo %var_1%
echo %var_1:~6%
set var_2=%%b
echo %var_2%
echo %var_2:~6%
)
When I run it the first time, the result is:
The output is:
string1a
string1b
ECHO is off.
~6
ECHO is off.
~6
The output is:
string2a
string2b
ECHO is off.
~6
ECHO is off.
~6
The output is:
string3a
string3b
ECHO is off.
~6
ECHO is off.
~6
When I run it a second time, the result is:
The output is:
string1a
string1b
string3a
3a
string3b
3b
The output is:
string2a
string2b
string3a
3a
string3b
3b
The output is:
string3a
string3b
string3a
3a
string3b
3b
If I then change the last line of file.txt
to:
string3x,string3y
and run the batch file again, the result is:
The output is:
string1a
string1b
string3a
3a
string3b
3b
The output is:
string2a
string2b
string3a
3a
string3b
3b
The output is:
string3x
string3y
string3a
3a
string3b
3b
If I then run the batch file again the result is:
The output is:
string1a
string1b
string3x
3x
string3y
3y
The output is:
string2a
string2b
string3x
3x
string3y
3y
The output is:
string3x
string3y
string3x
3x
string3y
3y
Can anybody explain what's going on here? The result I'm expecting in the first place is:
The output is:
string1a
string1b
string1a
1a
string1b
1b
The output is:
string2a
string2b
string2a
2a
string2b
2b
The output is:
string3a
string3b
string3a
3a
string3b
3b
Somehow the variable doesn't seem to get set until the third iteration of the FOR
loop, and then it lingers on in the next run of batch file. I'm trying to set var_1
and var_2
in each iteration to the values of %%a
and %%b
in that current iteration.