Calling Subroutines

May 7, 2009 at 13:07:12
Specs: Windows XP
I am trying to redesign an older batch file and need to call twenty new subroutines cleanly.

This sample code works, but there has to be a better way:

@echo off
for %%f in (1 2 3 4) do call :sub%%f
GOTO :Done

:sub1
echo this is :sub1
GOTO:EOF

:sub2
echo this is :sub2
GOTO:EOF

:sub3
echo this is :sub3
GOTO:EOF

:sub4
echo this is :sub4
GOTO:EOF

:Done
echo finished


See More: Calling Subroutines

Report •


#1
May 7, 2009 at 13:20:38
From what you posted there is just one thing to be improved:

  for /L %%f in (1 1 4) do call :sub%%f


Report •

#2
May 7, 2009 at 14:05:31
The old code redirected each subroutine to an output file. The following redirect does not work.

for /L %%f in (1 1 4) do call :sub%%f > %report%

What am I doing wrong?



Report •

#3
May 7, 2009 at 14:53:12
Not sure, try

for /L %%f in (1 1 4) do (call :sub%%f) >> %report%


Report •

Related Solutions

#4
May 7, 2009 at 19:04:30
This snippet below produces the error "syntax of the command is incorrect."

So there is a problem with how it is redirecting the output using >> %REPORTS%

for /L %%f in (1, 1, 20) do (call :CollectionSubroutine%%f) >> %REPORTS%



Report •

#5
May 8, 2009 at 06:04:11
I tested the suggested code and found it works in both ways, i.e.

for /L %%f in (1 1 4) do (call :sub%%f) >> %report%

or

for /L %%f in (1 1 4) do call :sub%%f >> %report%

So the problem is how you set the variable report, i.e. does it hold a valid Pathname/Filename?

This answer you have to discover by yourself.



Report •


Ask Question