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.
This question already has answers here:
Loop through input parameters and array? [duplicate]
Arrays, linked lists and other data structures in cmd.exe (batch) script
(11 answers)
Closed 2 years ago.
I need to loop through the list of files set through the command line, and loop through a local array.
No knowing of a better way, I tried using labels, but cmd doesn't behave as expected:
#echo off
if "%~1"=="" GOTO PARAM
setlocal enableextensions enabledelayedexpansion
set colors[0]="<style>yellow</style>"
set colors[1]="<style>blue</style>"
set COUNTER=0
for %%f in ("%1") DO (
echo Handling %%f
echo !COUNTER!
:LOOP
IF !COUNTER!=="0" GOTO CASE_YELLOW
IF !COUNTER!=="1" GOTO CASE_BLUE
IF !COUNTER! GTR 6 GOTO END
set /a COUNTER +=1
)
GOTO END
:CASE_YELLOW
ECHO Case YELLOW
GOTO LOOP
:CASE_BLUE
ECHO Case BLUE
GOTO LOOP
:PARAM
echo Usage : %0 myfile.xml/*.xml
:END
ECHO Done.
Here's the output using "myscript.bat file*.xml":
Handling file1.xml
0
Handling fileé.xml
1
Done.
Thank you.
Thankfully you put enough info in your code commenting to kinda see where you were going to with the arguments.
that said, if you want to do something when a condition is met in a loop, and continue with the loop after doing it, you simply need to change to calling your sub Functions, and then end of file which will return tou to your place in the loop.
this should do the needful
#(setlocal enableextensions enabledelayedexpansion
echo off
set "colors[0]=<style>yellow</style>"
set "colors[1]=<style>blue</style>"
set /a "COUNTER=0"
SET "SRC=%~1"
)
IF /I "%~1" EQU "" (
Call :PARAM
) ELSE (
CALL :Main
)
( ENDLOCAL
CALL :End
EXIT /B 0
)
:Main
For %%f in ("%SRC%") DO (
IF !COUNTER! < 6 (
echo Handling %%f
echo !COUNTER!
CALL ECHO.IN MAIN LOOP COLOR= "%%Colors[%counter%]%%"
IF !COUNTER!==0 CALL :CASE_YELLOW
IF !COUNTER!==1 CALL :CASE_BLUE
set /a "COUNTER+=1"
) ELSE (
echo.exiting.
GOTO :EOF
)
)
GOTO :EOF
:CASE_YELLOW
ECHO Case YELLOW
ECHO.In sub function yellow COLOR = "!Colors[%counter%]!"
GOTO :EOF
:CASE_BLUE
ECHO Case BLUE
ECHO.In sub function blue COLOR= "!Colors[%counter%]!"
GOTO :EOF
:PARAM
echo Usage : %0 myfile.xml/*.xml
GOTO :EOF
:END
ECHO Done.
Here's the out
GOTO :EOF
I am looking for a way to get a value return from a start-command launched batch script. Let me explain:
I need to take advantage of multiprocessing by launching multiple sub-batch scripts simultaneously from a main batch script, then retrieve every sub batch file return value when they're done.
I've been using return variables with the call-command as very well explained by dbenham.
That solution does not allow multithreading, since sub-batch scripts are run one after the other.
Using the start-command allows multiple running batch scripts, but values are not returned to my main script because apparently the start-command creates a whole new variable context.
Does anybody have a solution/workaround to return values from the sub-scripts to the main script ?
Below is a model of what I need:
mainScript.bat
#echo off
setlocal
set "retval1=0"
set "retval2=0"
REM run two scripts in parallel:
start "" subscript1.bat arg1 retval1
start "" subscript2.bat arg1 retval2
REM wait for returned value
:waiting
call :sleep 1
set /a "DONE=1"
if %retval1% equ 0 set "DONE=0"
if %retval2% equ 0 set "DONE=0"
if %DONE% equ 0 goto :waiting
echo returned values are %retval1% %retval2%
exit /b
subscript1.bat
#echo off
setlocal
set "arg1=%~1"
set "retval1=%~1"
REM do some stuff...
REM return value
(endlocal
set "%retval1%=%foo%"
)
exit /b
Can't see any alternative to writing your return values to files, so
main
#ECHO OFF
SETLOCAL
for %%a in (1 2) do (
del "%temp%\retval%%a" 2>nul
)
start /min "" q225220791.bat arg1 retval1
choice /t 1 /d y >nul
start /min "" q225220791.bat arg1 retval2
:waiting
choice /t 1 /d y >nul
ECHO wait...%retval1%...%retval2%
if not exist "%temp%\retval1" goto waiting
if not exist "%temp%\retval2" goto waiting
for %%a in (1 2) do (
for /f "usebackqdelims=" %%i in ("%temp%\retval%%a") do set "retval%%a=%%i"
)
for %%a in (1 2) do (
del "%temp%\retval%%a" 2>nul
)
echo returned values are %retval1% %retval2%
GOTO :EOF
q225220791.bat
#ECHO OFF
SETLOCAL
:: wait a random time 2..10 sec.
SET /a timeout=%RANDOM% %% 8 + 2
choice /t %timeout% /d y >nul
:: return a random result 12..20
SET /a foo=%RANDOM% %% 8 + 12
>"%temp%\%2" echo %foo%
ENDLOCAL
exit
Relying on the value of the second parameter to the sub-process to set the tempfile name. I've changed the names of the batches to suit my system.
Not sure if this is even practical, but just testing to avoid the temporary file. So looking for a place in child process that can be readed from parent process i decided to use the window title.
task.cmd
#echo off
setlocal
rem Retrieve task information and set window title
set "taskID=%~1"
title [task];%taskID%;working;
rem Retrieve the rest of parameters. For this sample, a random value
set /a "timeToWait=%~2 %% 30"
rem Simulate command processing
timeout /t %timeToWait%
rem Calculate a return value for this task
for /f "tokens=1-10 delims=,.:/ " %%a in ("%date%%time%_%~2") do set "returnValue=%%a%%b%%c%%d%%e%%f%%g%%h%%i%%j"
rem Signal the end of the task
title [task];%taskID%;ended; my return value is %returnValue% ;
rem Wait for master to end this tasks
cls
echo Waiting for master....
waitfor %taskID%
rem Cleanup
endlocal
master.cmd
#echo off
setlocal enableextensions enabledelayedexpansion
rem Configure tasks
set "taskPrefix=myTSK"
set "numTasks=5"
rem Start tasks
for /l %%a in (1 1 %numTasks%) do (
set "return[%taskPrefix%%%a]=unknown"
start "[task];%taskPrefix%%%a;working;" cmd /c "task.cmd %taskPrefix%%%a !random!"
)
rem Wait for tasks to start
timeout /t 2 > nul
rem Wait for tasks to end. Get the list of cmd.exe windows with window title
rem to see the state of the task
rem Tasks in working state indicate master needs to keep working
rem Tasks in ended state have the return value in the window title and are
rem waiting for the master to retrieve the value and end them
:wait
set "keepWaiting="
for /f "tokens=9 delims=," %%a in ('tasklist /fi "imagename eq cmd.exe" /fo csv /v ^| findstr /l /c:"[task];%taskPrefix%"'
) do for /f "tokens=2-4 delims=;" %%b in ("%%a") do (
if "%%c"=="working" (
set "keepWaiting=1"
) else if "%%c"=="ended" (
set "return[%%b]=%%d"
start "" /min waitfor.exe /si %%b
)
)
rem If any task has been found in working state, keep waiting
if defined keepWaiting (
echo %time% : waiting ...
timeout /t 5 > nul
goto wait
)
rem All tasks have ended. Show return values
for /l %%a in (1 1 %numTasks%) do (
echo task #%%a ended with exit value :[!return[%taskPrefix%%%a]!]
)
endlocal
In this sample, the task waits for master using waitfor command. In XP this is not available, and can be replaced with just a pause, and from master.cmd the waiting loop must be modified to include the processID token in the tasklist processing, so the waiting task can be closed with taskkill.
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.
I need to execute a command 100-200 times, and so far my research indicates that I would either have to copy/paste 100 copies of this command, OR use a for loop, but the for loop expects a list of items, hence I would need 200 files to operate on, or a list of 200 items, defeating the point.
I would rather not have to write a C program and go through the length of documenting why I had to write another program to execute my program for test purposes. Modification of my program itself is also not an option.
So, given a command, a, how would I execute it N times via a batch script?
Note: I don't want an infinite loop
For example, here is what it would look like in Javascript:
var i;
for (i = 0; i < 100; i++) {
console.log( i );
}
What would it look like in a batch script running on Windows?
for /l is your friend:
for /l %x in (1, 1, 100) do echo %x
Starts at 1, steps by one, and finishes at 100.
WARNING: Use %% instead of %, if it's in a batch file, like:
for /l %%x in (1, 1, 100) do echo %%x
(which is one of the things I really really hate about windows scripting.)
If you have multiple commands for each iteration of the loop, do this:
for /l %x in (1, 1, 100) do (
echo %x
copy %x.txt z:\whatever\etc
)
or in a batch file
for /l %%x in (1, 1, 100) do (
echo %%x
copy %%x.txt z:\whatever\etc
)
Key:
/l denotes that the for command will operate in a numerical fashion, rather than operating on a set of files
%x is the loops variable
(starting value, increment of value, end condition[inclusive] )
And to iterate on the files of a directory:
#echo off
setlocal enableDelayedExpansion
set MYDIR=C:\something
for /F %%x in ('dir /B/D %MYDIR%') do (
set FILENAME=%MYDIR%\%%x\log\IL_ERROR.log
echo =========================== Search in !FILENAME! ===========================
c:\utils\grep motiv !FILENAME!
)
You must use "enableDelayedExpansion" and !FILENAME! instead of $FILENAME$. In the second case, DOS will interpret the variable only once (before it enters the loop) and not each time the program loops.
Template for a simple but counted loop:
set loopcount=[Number of times]
:loop
[Commands you want to repeat]
set /a loopcount=loopcount-1
if %loopcount%==0 goto exitloop
goto loop
:exitloop
Example: Say "Hello World!" 5 times:
#echo off
set loopcount=5
:loop
echo Hello World!
set /a loopcount=loopcount-1
if %loopcount%==0 goto exitloop
goto loop
:exitloop
pause
This example will output:
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Press any key to continue . . .
You could also try this instead of a for loop:
set count=0
:loop
set /a count=%count%+1
(Commands here)
if %count% neq 100 goto loop
(Commands after loop)
It's quite small and it's what I use all the time.
You could do something to the following effect avoiding the FOR loop.
set counter=0
:loop
echo "input commands here"
SET /A counter=%counter%+1
if %counter% GTR 200
(GOTO exit) else (GOTO loop)
:exit
exit
Or you can decrement/increment a variable by the number of times you want to loop:
SETLOCAL ENABLEDELAYEDEXPANSION
SET counter=200
:Beginning
IF %counter% NEQ 0 (
echo %x
copy %x.txt z:\whatever\etc
SET /A counter=%counter%-1
GOTO Beginning
) ELSE (
ENDLOCAL
SET counter=
GOTO:eof
Obviously, using FOR /L is the highway and this is the backstreet that takes longer, but it gets to the same destination.
Very basic way to implement looping in cmd programming using labels
#echo off
SET /A "index=1"
SET /A "count=5"
:while
if %index% leq %count% (
echo The value of index is %index%
SET /A "index=index + 1"
goto :while
)
You can do this without a for statement ^.^:
#echo off
:SPINNER
SET COUNTP1=1
:1
CLS
:: YOUR COMMAND GOES HERE
IF !COUNTP1! EQU 200 goto 2
SET COUNTP1=1
) ELSE (
SET /A COUNTP1+=1
)
goto 1
:2
:: COMMAND HAS FINISHED RUNNING 200 TIMES
It has basic understanding. Just give it a test. :P
DOS doesn't offer very elegant mechanisms for this, but I think you can still code a loop for 100 or 200 iterations with reasonable effort. While there's not a numeric for loop, you can use a character string as a "loop variable."
Code the loop using GOTO, and for each iteration use SET X=%X%# to add yet another # sign to an environment variable X; and to exit the loop, compare the value of X with a string of 100 (or 200) # signs.
I never said this was elegant, but it should work!
I use this. It is just about the same thing as the others, but it is just another way to write it.
#ECHO off
set count=0
:Loop
if %count%==[how many times to loop] goto end
::[Commands to execute here]
set count=%count%+1
goto Loop
:end
The answer really depends on how familiar you are with batch, if you are not so experienced, I would recommend incrementing a loop variable:
#echo off
set /a loop=1
:repeat
echo Hello World!
set /a loop=%loop%+1
if %loop%==<no. of times to repeat> (
goto escapedfromrepeat
)
goto repeat
:escapedfromrepeat
echo You have come out of the loop
pause
But if you are more experienced with batch, I would recommend the more practical for /l %loop in (1, 1, 10) do echo %loop is the better choice.
(start at 1, go up in 1's, end at 10)
for /l %[your choice] (start, step, end) do [command of your choice]
a completely flawless loop
set num=0
:loop
:: insert code
set /a num=%num%+1
if %num% neq 10 goto loop
::insert after code code
you can edit it by changing the 10 in line 5 to any number to represent how many time you want it to loop.
Not sure if an answer like this has already been submitted yet, but you could try something like this:
#echo off
:start
set /a var+=1
if %var% EQU 100 goto end
:: Code you want to run goes here
goto start
:end
echo var has reached %var%.
pause
exit
The variable %var% will increase by one until it reaches 100 where the program then outputs that it has finished executing. Again, not sure if this has been submitted or something like it, but I think it may be the most compact.
Use FOR /l and make sure to use %% instead of %
It will save you headaches.
And try to Set the loop.
(EDITED) I made it so it stops after 100 times
#echo off
goto actual
set /a loopcount=0
:actual
set /a loopcount=%loopcount% + 1
echo %random% %random% %random% %random%
timeout 1 /nobreak>nul
if %loopcount%== 100 goto stop
goto actual
:stop
exit
This will generate 4 random numbers ever 1 second 100 times.
Take out the "timeout 1 /nobreak>nul" to make it go super fast.
I have 2 answers
Methods 1:
Insert Javascript into Batch
#if (#a==#b) #end /*
:: batch portion
#ECHO OFF
cscript /e:jscript "%~f0"
:: JScript portion */
Input Javascript here
( I don't know much about JavaScript )
Method 2:
Loop in Batch
#echo off
set loopcount=5
:loop
echo Hello World!
set /a loopcount=loopcount-1
if %loopcount%==0 goto exitloop
goto loop
:exitloop
pause
(Thanks FluorescentGreen5)