Reading values in a text file and subsequent processing - windows

A text file contains values. These values are to be used as an argument to an executable.
I tried the following to see how I can use inputs (line by line) from a file:
#echo off
for /f "tokens=*" %%i in (test.txt) do (
set n1=%%i
echo %n1%
echo "done"
)
test.txt contains numbers: Ex.
0.1
0.002
3
20
The output of the set of batch commands processed from a batch file is:
20
"done"
20
"done"
20
"done"
20
"done"
What went wrong here ?

To access variables inside a code block you need delayed expansion:
#ECHO OFF &SETLOCAL ENABLEDELAYEDEXPANSION
for /f "DELIMS=" %%i in (test.txt) do (
set "n1=%%~i"
echo !n1!
echo "done"
)
Please note: delayed expanded variables need exclams instead of percents.
In this part of code you do not need delayed expansion if you use the for loop parameter %%i as "variable":
#ECHO OFF &SETLOCAL
for /f "DELIMS=" %%i in (test.txt) do (
echo %%i
echo "done"
)
But you cannot make string conversion like set "n1=!n1:.0=.!" with %%i.

Related

Take text from a text file and put each line into a variable

I have a problem taking a text file with 2 lines of text inside of it, and converting each line of the file into a separate variable.
I thought I could just repeat the same process for getting one line of text and just add skip=1 next to delims= and change the name of the variable it goes into, this did not work how I planned and I can't figure out another way to do it.
Here is my code:
#echo off
for /f "delims=" %%a in (file.txt) do (
set line1=%%a
)
for /f "skip=1 delims=" %%a in (file.txt) do (
set line2=%%a
)
echo %line1%
echo %line2%
pause
The text inside the file file.txt is this:
This is the Text on line 1
This is the Text on line 2
Instead of what i thought the output was going to be -
This is the Text on line 1
This is the Text on line 2
Press any key to continue...
It was this:
This is the Text on line 2
This is the Text on line 2
Press any key to continue...
It just prints the second line of the text file for both of the echos!
Please respond to this question as me not knowing how to do this is really bugging me
Thanks, Alex
This is a simple way:
#echo off
< file.txt ( set /P "line1=" & set /P "line2=" )
echo %line1%
echo %line2%
If you want to read more lines, or a variable number of lines, I suggest you to read Arrays, linked lists and other data structures in cmd.exe (batch) script
Take a look at this:
vars.txt (file with variables):
var1
var2
var3
tast.bat:
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET varFile=vars.txt
SET i=1
FOR /F %%l IN (%varFile%) DO (
SET line!i!=%%l
SET /a i=!i!+1
)
ECHO !line1!
ECHO !line3!
PAUSE
Output:
var1
var3
This will work regardless how many lines/variables you have. If you have only one, it will be stored in !line1!, if you have 1000, the 500th line will be stored in !line500! and so on.
This should work for lines with spaces by the help of tokens=*
#echo off
rem Creating env testing
(echo First blank line should be skiped
for /l %%i in (1,1,30) do ( echo This is the Text on line %%i)
)>_vars.tmp
setlocal enabledelayedexpansion
set i=1
for /F "skip=1 tokens=*" %%a in (_vars.tmp) do (
set line!i!=%%a
set /a i+=1
)
echo:!line1!
echo:!line3!
echo:!line30!
echo:!line100!
echo checking _vars.tmp
more _vars.tmp
del _vars.tmp
pause
exit /b 0

Why isn't my BAT file variable being overwritten?

I have this block of code:
#ECHO OFF
SET "SRCFOLDER=C:\Users\MyUserName\Desktop\PhotoTests"
SET "TEMPCODE=Hi"
ECHO %TEMPCODE%
ECHO.
FOR /F "tokens=*" %%G IN ('DIR /B %SRCFOLDER%') DO (
ECHO %%G
CALL tooltipInfo.bat %%G 19 | FIND /C "Star" > test.txt
SET /P TEMPCODE=<test.txt
ECHO %TEMPCODE%
ECHO.
)
SET /P TEMPCODE=<test.txt
ECHO %TEMPCODE%
ECHO.
PAUSE
I'm confused by the output as I noticed that the variable in the FOR loop is not overwritten by what I think should be the content of "test.txt", which will vary each time the FOR loop runs.
For the purpose of this example, the file tooltipInfo.bat will echo a text string like "1 Star" or "3 Stars" based on the rating recorded in the file's properties. The FIND statement should result in a "0" or "1" being saved into the test.txt file.
The output is:
Hi
Canon_Locked.JPG
Hi
Nikon_Locked.JPG
Hi
0
Press any key to continue . . .
May I know why the TEMPCODE variable isn't being overwritten in the loop and retains the original value of "Hi". However in the final block of code, it was able to read and echo out the actual content of the file.
This is a common mistake when using FOR and parentheses. The problem is that by default every variable between ( and ) is evaluated when the line is first read, and then re-used for each iteration of the FOR.
If the variable changes during the loop (via SET), then you will need to change % to ! for variables inside the parentheses, and also turn on delayed expansion using setlocal
SETLOCAL enabledelayedexpansion
FOR /F "tokens=*" %%G IN ('DIR /B %SRCFOLDER%') DO (
ECHO %%G
CALL tooltipInfo.bat %%G 19 | FIND /C "Star" > test.txt
SET /P TEMPCODE=<test.txt
ECHO !TEMPCODE!
ECHO.
)

pass parameters to windows batch file multiple times

I m having issues in passing parameters to a batch file.A parameter file will have n number of lines and i want to execute the bacth to read the first line,take that as a parameter in .bat and execute.Read the next line take it as second parameter execute again.Likewise it should execute n number of times if it finds n number of lines in text file.(For example if the text file have 100 lines the loop execution in .bat should continue for 100 times).
I have script like,
#echo off
setlocal enabledelayedexpansion
set file1=D:\Batch\parm.txt
set /a cnt=0
for /f "tokens=*" %%a in (%file1%) do (
set %file1% =%%a
echo !%file1%!
)
FOR /F "tokens=1 delims=|" %%G IN (%file1%) DO set a1=%%G
FOR /F "tokens=2 delims=|" %%K IN (%file1%) DO set a2=%%K
FOR /F "tokens=3 delims=|" %%I IN (%file1%) DO set a3=%%I
echo parameter file found
echo reading parameters to pass through
echo (%a1%,%a2%,%a3%)>>D:\Batch\output.txt
goto break
:break
set /a cnt+=1
exit /b
my parameter file has input as
"India"|"Australia"|"Africa"
"I1"|"A1"|"A11"
"I2"|"A2"|"A12"
my output should be:
parameter file found
reading parameters to pass through
"India","Australia","Africa"
parameter file found
reading parameters to pass through
"I1","A1","A11"
parameter file found
reading parameters to pass through
"I2","A2","A12"
i m currently getting only last parameter as output.Please help me to correct the script.
Your first FOR loop is crazy - it attempts to create a variable with a name that matches its value. I don't see how it serves any purpose.
Your logic is all wrong for each parameter. You read the entire file for the first parameter in a loop. When that loop is finished, you only have one parameter value for the last line found. You then do the same process for the 2nd and 3rd parameters. That can't work.
You should read all 3 parameters in a single loop.
#echo off
setlocal
set "file1=D:\Batch\parm.txt"
if exist "%file1%" (
echo parameter file found
echo reading parameters to pass through
set /a cnt=0
for /f "usebackq tokens=1-3 delims=|" %%A IN ("%file1%") do (
echo (%%A,%%B,%%C^)
set /a cnt+=1
)
)>d:\batch\output.txt
echo cnt=%cnt%
exit /b
#echo off
setlocal enabledelayedexpansion
set file1=D:\Batch\parm.txt
set /a cnt=0
for /f "tokens=*" %%a in (%file1%) do (
set %file1%=%%a
echo !%file1%!
)
FOR /F "tokens=1,2,3 delims=|" %%G IN (%file1%) DO set a1=%%a&set a2=%%b&set a3=%%c
echo parameter file found>>D:\Batch\output.txt
echo reading parameters to pass through>>D:\Batch\output.txt
echo (%a1%,%a2%,%a3%)>>D:\Batch\output.txt
goto break
:break
set /a cnt+=1
exit /b

How to set for loop variable? Batch file

I am unable to set a variable value inside for loop.
#echo off
SET PID=CREE6-GGGG8-FFFF6-SSSS9-DDDD5
FOR %%x in (%PID:-= %) do (
echo %%x
SET v=(echo %%x| convert_2_scancode.py)
echo %v%
)
Expected o/p
CREE6
12 92 2e 23.......... (converted scan code of CREE6 )
For one thing... you are echoing the 'Load Time' value of the variable v. To see the 'Run Time' value within a FOR/IF construct or within parens you need to add this line (2nd line of your program).
setlocal enabledelayedexpansion
and then change your echo %v% to
echo !v!
Then there is the next problem. What are you attempting to do with
SET v=(echo %%x| convert_2_scancode.py)
I'd try
#echo off
SET PID=CREE6-GGGG8-FFFF6-SSSS9-DDDD5
FOR %%x in (%PID:-= %) do (
echo %%x
for /f Delims=" %%v in ('echo %%x^|convert_2_scancode.py') do set v=%%v
)
echo %v%
As I don't have "convert_2_scancode.py", I wrote a little "emulator" instead
C:\Users\Stephan\102>type t.bat
#echo off
SET PID=CREE6-GGGG8-FFFF6-SSSS9-DDDD5
set pid=%PID:-= %
for /f "tokens=* delims=" %%x in ('convert_2_scancode.bat %pid%') do set "v=%%x"
echo %v%
------------------
C:\Users\Stephan\102>type convert_2_scancode.bat
#echo converted scancode from %*
------------------
C:\Users\Stephan\102>t
converted scancode from CREE6 GGGG8 FFFF6 SSSS9 DDDD5
C:\Users\Stephan\102>

Errorlevel of command executed by batch for loop

The following code always displays 0 as the errorlevel, but when the copy command is done outside of the for loop command it returns a non zero errorlevel.
for /f "usebackq delims=" %%x in (`copy x y`) do (
set VAR=%%x
)
ECHO Errorlevel = %ERRORLEVEL%
ECHO VAR = %VAR%
Is is possible to get the errorlevel of the copy command executed by the for loop?
it works for me ! You only need to put the error checking within the DO parentheses
with a text file containing the copy commands (7200 lines; for example:
copy 2_97691_Scan.pdf O:\Data\Dev\Mins\PDFScan2\2011\4\2_97691_Scan.pdf),
I can run the following batch file
#echo off
setlocal EnableDelayedExpansion
for /F "delims=" %%I in (CopyCurrentPDFs.txt) do (
%%I
if !errorlevel! NEQ 0 echo %%I>>errorcopy.txt
)
I am assuming that you are copying files from one directory to another? If so, you could do something like this instead:
#echo off
setlocal EnableDelayedExpansion
set ERR=0
for %%x in (x) do (
copy %%x y
set ERR=!errorlevel!
set VAR=%%x
)
ECHO Errorlevel = %ERR%
ECHO VAR = %VAR%
The delayed expansion is required to get the actual value of errorlevel inside the loop instead of the value before the loop is entered.
If that isn't what you are trying to do, please clarify your objective.

Resources