Errorlevel of command executed by batch for loop - windows

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.

Related

Return a value from a called batch file label

I have two labels in my batch file. The initial label MAIN shall stay in control, so it Calls the second label, which ends with exit /b.
My script's Main label Calls the other, passing it arguments, which will be used to search strings wothin a text file.
When returning to the Calling label, it slways receives an empty return string.
I think this has something to do with the variable expansion in a loop. Who knows?
Here is the Script:
#echo off
SETLOCAL ENABLEDELAYEDEXPANSION
:MAIN
call :getReturnValue "1234 0815 4321 12815" "readBackVal"
if !errorlevel! equ 0 (
echo readback=!readBackVal!
echo readback=%readBackVal%
)
pause
exit /b 0
REM Function, which checks if the give return value is in a specific textfile (line for line check)
:getReturnValue
set "myExpectedValueList=%~1"
set "retval=%~2"
set "file=textexample.txt"
for %%i in (%myExpectedValueList%) do (
for /f "tokens=*" %%a in (%file%) do (
echo %%a|findstr /r "^.*%%i$"
)
if !errorlevel! equ 0 (
(endlocal
set /a "%retval%=%%i")
)
exit /b 0
)
)
exit /b 1
Here is the sample textfile textexample.txt:
Setup returns with errorcode=0815
Here is the answer i looked for:
Hi, first i want to inform that i made some changes due to the Answer of
#OJBakker. This changes are listed at the bottom of the script.
The problem was to return a value from a called function/label to the calling function/label. The stich here is, that the magic
is done in the (endlocal...) section of the called function/label -> means the return of the variable.
Before the endlocal command is executed, the compiler replaces the variables in this section by their values and afterwards executes the command´s from left to right. Means following:
First, the compiler sees following:
(endlocal
if "%retval%" neq "" (call set /a %retval%=%%i)
)
Second, the compiler replaces the variables by their values:
(endlocal
if "readBackVal" neq "" (set /a "readBackVal"=1815)
)
Third: This command is executed
(endlocal
if "readBackVal" neq "" (set /a "readBackVal"=1815)
)
Now here is my complete script (i also fixed some other problems with it which i commented at the bottom of the script
#echo off
SETLOCAL ENABLEDELAYEDEXPANSION
:MAIN
setlocal
call :getReturnValue "1234 1815 4321 12815" "readBackVal"
if "!errorlevel!" equ "0" (
echo readback=!readBackVal!
)
pause
exit /b 0
REM Function, which checks if the give return value is in a specific textfile (line for line check)
:getReturnValue
setlocal
set "myExpectedValueList=%~1"
set "retval=%~2"
set "file=textexample.txt"
for %%i in (%myExpectedValueList%) do (
for /f "tokens=*" %%a in (%file%) do (
echo %%a|findstr /r "^.*%%i$" >NUL
)
if "!errorlevel!" equ "0" (
(endlocal
if "%retval%" neq "" (set /a %retval%=%%i)
)
exit /b 0
)
)
exit /b 1
REM Changes to initial posting:
REM Added "setlocal" keyword to the function "getReturnValue"
REM Corrected an invalid paranthesis in the (endlocal...) section
REM Changed the file "textexample.txt" -> 0815 to 1815 to remove leading zero (findstr. Problem),
REM Added check, if parameter "retval" has been passed to the called function e.g. is not empty
REM FINAL -> applied double variable expansion (call set /a ...) to return the value proper
REM to the :MAIN function.
#echo off
SETLOCAL ENABLEDELAYEDEXPANSION
:MAIN
call :getReturnValue "1234 0815 4321 12815" "readBackVal"
if %errorlevel% equ 0 (echo readback=%readBackVal%)
pause
endlocal
exit /b 0
REM Function, which checks if the give return value is in a specific textfile (line for line check)
:getReturnValue
set "myExpectedValueList=%~1"
set "retval=%~2"
set "file=textexample.txt"
for %%i in (%myExpectedValueList%) do (
for /f "tokens=*" %%a in (%file%) do (
echo %%a| >con 2>&1 findstr /r "^.*%%i$"
if !errorlevel! equ 0 (
set /a "%retval%=%%i"
exit /b 0
)
)
)
exit /b 1
rem changes:
rem endlocal moved to main.
rem check for errorlevel moved to within the commandblock of the inner for-loop.
rem 'exit /b 0' moved to within the if. This exit line stopped the for after the first item.
rem redirection added to findstr command. Now the output shows the remaining problem.
rem Invalid number. Numeric constants are either decimal (17), hexadecimal (0x11), or octal (021).
rem Findstr really does not like the value 0815, especially the starting zero.
rem I am not sure how to change the regexp so findstr won't barf at the leading zero.
rem Maybe someone else can solve this remaining problem.

Batch String +=? [duplicate]

I made this code
dir /B /S %RepToRead% > %FileName%
for /F "tokens=*" %%a in ('type %FileName%') do (
set z=%%a
echo %z%
echo %%a
)
echo %%a is working fine but echo %z% returns "echo disabled".
I need to set a %z% because I want to split the variable like %z:~7%
Any ideas?
There are two methods to setting and using variables within for loops and parentheses scope.
setlocal enabledelayedexpansion see setlocal /? for help. This only works on XP/2000 or newer versions of Windows.
then use !variable! instead of %variable% inside the loop...
Create a batch function using batch goto labels :Label.
Example:
for /F "tokens=*" %%a in ('type %FileName%') do call :Foo %%a
goto End
:Foo
set z=%1
echo %z%
echo %1
goto :eof
:End
Batch functions are very useful mechanism.
You probably want SETLOCAL ENABLEDELAYEDEXPANSION. See https://devblogs.microsoft.com/oldnewthing/20060823-00/?p=29993 for details.
Basically: Normal %variables% are expanded right aftercmd.exe reads the command. In your case the "command" is the whole
for /F "tokens=*" %%a in ('type %FileName%') do (
set z=%%a
echo %z%
echo %%a
)
loop. At that point z has no value yet, so echo %z% turns into echo. Then the loop is executed and z is set, but its value isn't used anymore.
SETLOCAL ENABLEDELAYEDEXPANSION enables an additional syntax, !variable!. This also expands variables but it only does so right before each (sub-)command is executed.
SETLOCAL ENABLEDELAYEDEXPANSION
for /F "tokens=*" %%a in ('type %FileName%') do (
set z=%%a
echo !z!
echo %%a
)
This gives you the current value of z each time the echo runs.
I struggeld for many hours on this.
This is my loop to register command line vars.
Example : Register.bat /param1:value1 /param2:value2
What is does, is loop all the commandline params,
and that set the variable with the proper name to the value.
After that, you can just use
set value=!param1!
set value2=!param2!
regardless the sequence the params are given. (so called named parameters).
Note the !<>!, instead of the %<>%.
SETLOCAL ENABLEDELAYEDEXPANSION
FOR %%P IN (%*) DO (
call :processParam %%P
)
goto:End
:processParam [%1 - param]
#echo "processparam : %1"
FOR /F "tokens=1,2 delims=:" %%G IN ("%1") DO (
#echo a,b %%G %%H
set nameWithSlash=%%G
set name=!nameWithSlash:~1!
#echo n=!name!
set value=%%H
set !name!=!value!
)
goto :eof
:End
Simple example of batch code using %var%, !var!, and %%.
In this example code, focus here is that we want to capture a start time using the built in variable TIME (using time because it always changes automatically):
Code:
#echo off
setlocal enabledelayedexpansion
SET "SERVICES_LIST=MMS ARSM MMS2"
SET START=%TIME%
SET "LAST_SERVICE="
for %%A in (%SERVICES_LIST%) do (
SET START=!TIME!
CALL :SOME_FUNCTION %%A
SET "LAST_SERVICE=%%A"
ping -n 5 127.0.0.1 > NUL
SET OTHER=!START!
if !OTHER! EQU !START! (
echo !OTHER! is equal to !START! as expected
) ELSE (
echo NOTHING
)
)
ECHO Last service run was %LAST_SERVICE%
:: Function declared like this
:SOME_FUNCTION
echo Running: %1
EXIT /B 0
Comments on code:
Use enabledelayedexpansion
The first three SET lines are typical
uses of the SET command, use this most of the time.
The next line is a for loop, must use %%A for iteration, then %%B if a loop inside it
etc.. You can not use long variable names.
To access a changed variable such as the time variable, you must use !! or set with !! (have enableddelayexpansion enabled).
When looping in for loop each iteration is accessed as the %%A variable.
The code in the for loop is point out the various ways to set a variable. Looking at 'SET OTHER=!START!', if you were to change to SET OTHER=%START% you will see why !! is needed. (hint: you will see NOTHING) output.
In short !! is more likely needed inside of loops, %var% in general, %% always a for loop.
Further reading
Use the following links to determine why in more detail:
Difference between %variable% and !variable! in batch file
Variable usage in batch file
To expand on the answer I came here to get a better understanding so I wrote this that can explain it and helped me too.
It has the setlocal DisableDelayedExpansion in there so you can locally set this as you wish between the setlocal EnableDelayedExpansion and it.
#echo off
title %~nx0
for /f "tokens=*" %%A in ("Some Thing") do (
setlocal EnableDelayedExpansion
set z=%%A
echo !z! Echoing the assigned variable in setlocal scope.
echo %%A Echoing the variable in local scope.
setlocal DisableDelayedExpansion
echo !z! &rem !z! Neither of these now work, which makes sense.
echo %z% &rem ECHO is off. Neither of these now work, which makes sense.
echo %%A Echoing the variable in its local scope, will always work.
)
set list = a1-2019 a3-2018 a4-2017
setlocal enabledelayedexpansion
set backup=
set bb1=
for /d %%d in (%list%) do (
set td=%%d
set x=!td!
set y=!td!
set y=!y:~-4!
if !y! gtr !bb1! (
set bb1=!y!
set backup=!x!
)
)
rem: backup will be 2019
echo %backup%
Try this:
setlocal EnableDelayedExpansion
...
for /F "tokens=*" %%a in ('type %FileName%') do (
set z=%%a
echo !z!
echo %%a
)
You can use a macro if you access a variable outside the scope
#echo off
::Define macro
set "sset=set"
for /l %%a in (1,1,4) do (
::set in loop
%sset% /a "x[%%a]=%%a*%%a"
if %%a equ 4 (
:: set in condition
%sset% "x[%%a]=x Condition"
%sset% "y=y Condition"
)
)
echo x1=%x[1]% x2=%x[2]% x3=%x[3]% x4=%x[4]% y=%y%
:: Bonus. enableDelayedExpansion used to access massive from the loop
setlocal enableDelayedExpansion
echo Echo from the loop
for /l %%a in (1,1,4) do (
::echo in one line - echo|set /p =
echo|set /p "=x%%a=!x[%%a]! "
if %%a equ 4 echo y=%y%
)
pause
I know this isn't what's asked but I benefited from this method, when trying to set a variable within a "loop". Uses an array. Alternative implementation option.
SETLOCAL ENABLEDELAYEDEXPANSION
...
set Services[0]=SERVICE1
set Services[1]=SERVICE2
set Services[2]=SERVICE3
set "i=0"
:ServicesLoop
if defined Services[%i%] (
set SERVICE=!Services[%i%]!
echo CurrentService: !SERVICE!
set /a "i+=1"
GOTO :ServicesLoop
)
The following should work:
setlocal EnableDelayedExpansion
for /F "tokens=*" %%a in ('type %FileName%') do (
set "z=%%a"
echo %z%
echo %%a
)

Reading values in a text file and subsequent processing

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.

windows batch script - setting variable inside for loop

maybe i'm not representing my question clear, here's the actual code i did:
#echo off
set /p keywords="Enter keywords to search: " %=%
dir /b *.dat > filelist.txt
for /f "delims=." %%f in (filelist.txt) do (
for /f "delims= " %%g in (%%f.dat) do (
7z e %%g *sec.evtx
dir /b *.evtx > evtfile.txt
set /p tmpvar1=<evtfile.txt
del *.evtx
)
)
filelist.txt
tsnint1.dat
webint1.dat
tsnint1.dat
TSNINT1-201312091700.zip
TSNINT1-201312091600.zip
TSNINT1-201312091500.zip
TSNINT1-201312091400.zip
TSNINT1-201312091300.zip
TSNINT1-201312091200.zip
webint1.dat
WEBINT1-201312091300.zip
WEBINT1-201312091200.zip
the problem i'm facing is, evtfile consists of the right content but tmpvar1 is not assigned correctly as expected, what is my mistake and how to correct it? many thanks
You need delayed expansion to use a variable inside a block when this variable has been set (or changed) inside the same block. But you can set a variable without delayed expansion.
See this little demonstration (I used a simple if construct instead of for, but the effect is the same (not with if or for, but with blocks (inside ( and )).
#echo off
REM SETTING a variable inside a block
set "var=ONE"
echo start: %var%
if 1==1 (
echo inside block: %var%
set var=TWO
echo var has a new value:
set var
echo inside block is still old: %var%
)
echo after block: %var%
echo ----------
REM USING a variable inside a block
setlocal enabledelayedexpansion
set "var=ONE"
echo start: %var%
if 1==1 (
echo inside block: %var%
set var=TWO
echo var has a new value:
set var
echo new value inside block: !var!
echo just to demonstrate: %var%
)
echo after block: %var%
endlocal
echo ----------
echo working fine: %var%
echo not available after endlocal: !var!
This might help, using your code as a base. FWIW I hope that you have more than one copy of the .evtx files, if they are important to you.
#echo off
setocal enabledelayedexapnsion
for /f "delims=" %%f in ('dir /b *.dat') do (
for /f "delims=" %%g in ('type "%%f" ') do (
7z e "%%g" "*sec.evtx"
dir /b *.evtx > evtfile.txt
set /p tmpvar1=<evtfile.txt
echo !tempvar!
del *.evtx
)
)
One issue is that the *.evtx files are being deleted after the first archive is created, so the subsequent ones will have nothing to archive.

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

Resources