You are not logged in.

#1 07 Dec 2012 15:09

npocmaka
Member
From: Bulgaria
Registered: 03 Dec 2009
Posts: 446

Calling subroutine in FOR /F definition

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

#2 07 Dec 2012 16:13

carlos
Member
From: Chile
Registered: 04 Nov 2008
Posts: 251
Website

Re: Calling subroutine in FOR /F definition

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

#3 07 Dec 2012 18:06

npocmaka
Member
From: Bulgaria
Registered: 03 Dec 2009
Posts: 446

Re: Calling subroutine in FOR /F definition

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

#4 08 Dec 2012 21:52

Aacini
Member
Registered: 05 Dec 2012
Posts: 149

Re: Calling subroutine in FOR /F definition

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

#5 09 Dec 2012 02:16

dbenham
Member
From: U.S. east coast
Registered: 15 Apr 2012
Posts: 111

Re: Calling subroutine in FOR /F definition

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

#6 11 Dec 2012 10:56

npocmaka
Member
From: Bulgaria
Registered: 03 Dec 2009
Posts: 446

Re: Calling subroutine in FOR /F definition

thanks. This is what I needed smile

Offline

Board footer

Powered by