After I turned off the echoing of commands run, by adding @echo off
at the top of the script, I had no issues with the script. I created a file called file.txt
, pasted the mock line you provided, and placed this the directory stored in the tmp
environment variable. Once i ran the script, the var
environment variable contained the line found and it was also printed out as expected.
The script I used:
@echo off
cd /d %tmp%
set var1=1
if %var1%==1 (
for /F "delims=" %%a in ('findstr /I "start,ani" %tmp%\*.txt') do (
set "var=%%a"
echo %%a
echo %var%
)
)
This post has a bit more on the @
and echo off
.
Update: I'm not sure of your full scenario but this may be something you eventually run into since you're setting a variable and echoing it in a for loop. If you have two lines in a file with the token "start,ani" your output will look like the following (the file path has been removed and replaced with simply [file path]
):
[file path]:10.18.77.14.152392.16.APP_Ret,04/10/2018 17:21:07.592,,start,ani,1234567890
[file path]:11.18.77.14.152392.16.APP_Ret,04/10/2018 17:21:07.592,,start,ani,1234567890
[file path]:11.18.77.14.152392.16.APP_Ret,04/10/2018 17:21:07.592,,start,ani,1234567890
[file path]:11.18.77.14.152392.16.APP_Ret,04/10/2018 17:21:07.592,,start,ani,1234567890
As you can see the first echo of %var%
is the same as the last and this is because the variable is only expanded once. To get more expected output you would need to use delayed expansion:
@echo off
setlocal ENABLEDELAYEDEXPANSION
cd /d %tmp%
set var1=1
if %var1%==1 (
for /F "delims=" %%a in ('findstr /I "start,ani" %tmp%\*.txt') do (
set "var=%%a"
echo %%a
echo !var!
)
)
Now the first echo of var
will display as expected:
[file path]:10.18.77.14.152392.16.APP_Ret,04/10/2018 17:21:07.592,,start,ani,1234567890
[file path]:10.18.77.14.152392.16.APP_Ret,04/10/2018 17:21:07.592,,start,ani,1234567890
[file path]:11.18.77.14.152392.16.APP_Ret,04/10/2018 17:21:07.592,,start,ani,1234567890
[file path]:11.18.77.14.152392.16.APP_Ret,04/10/2018 17:21:07.592,,start,ani,1234567890
This post has more info on delayed expansion.