You are not logged in.
This does not work :
for /f %%P in ('call :subroutine params') do ( something %%P)
,but this works :
for /f %%P in (':subroutine params') do ( something %%P)
Can be useful to know I think...
Last edited by npocmaka (07 Dec 2012 18:06)
Offline
I always think that call subroutines from for /f was impossible. Very thanks for post the way.
Last edited by carlos (07 Dec 2012 16:18)
Offline
It does not work.I feel embarrassed ...
Every string started with : in for /f command is ignored and does not print an error which fooled me.
Last edited by npocmaka (07 Dec 2012 18:07)
Offline
A simple trick allows to call an internal subroutine from a FOR /F. Just precede the call with %0 and insert this line at beginning:
if /I "%1" equ "call" shift & shift & goto %2
For example:
@echo off
if /I "%1" equ "call" shift & shift & goto %2
echo Call the subroutine
for /F "delims=" %%a in ('%0 call :subroutine abc xyz') do echo %%a
echo Return from subroutine
goto :EOF
:subroutine
echo In the subroutine
echo Param 1: %1, Param 2: %2
exit /B
Output:
Call the subroutine
In the subroutine
Param 1: abc, Param 2: xyz
Return from subroutine
Antonio
Offline
Nice Aacini.
It is better to use "%~f0" for the following reasons:
1) The script may not be in the current directory.
2) The script path may include space or other characters that need to be quoted.
3) If run from within a CALLed subroutine, %0 will give the name of the called label, but %~f0 will give the full path to the batch script.
Dave Benham
Offline
thanks. This is what I needed
Offline