Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How do function calls WITH parameters work inside a DOS batch script ?

1,585 views
Skip to first unread message

Werner Sammer

unread,
Sep 6, 2006, 8:19:25 AM9/6/06
to
Inside a DOS batch script (in WinXP) I want to call several times a function/procedure.
This function/procedure should be in the same *.bat batch script as the calling statement
and take some parameters.

How can I implement such a function call (especially how do I work inside the function/procedure with
the received parameter values) ?

The following does NOT work (although I would appreciate to use it in a similar style):

mytest.bat:

set parm1=hello
set parm2=12345
goto mysub %parm1% %parm2%

set parm1=test
set parm2=7777
goto mysub %parm1% %parm2%

...

exit

:MYSUB %1 %2

Echo I got the following parameters: %1 and %2

goback


How do I do this otherwise ?
NT extensions can be used.

Werner

Marty List

unread,
Sep 6, 2006, 8:30:22 AM9/6/06
to

Use CALL instead of GOTO before your function name, run "call /?" to view
the syntax. At the end of the function, use "GoTo :EOF" to jump to the "end
of file" which will actually return you back to the next line after CALL.
Run "goto /?" to view the syntax.


set parm1=hello
set parm2=12345
Call :MYSUB %parm1% %parm2%

set parm1=test
set parm2=7777
Call :MYSUB %parm1% %parm2%

exit /b


:MYSUB


Echo I got the following parameters: %1 and %2

GoTo :EOF

"Werner Sammer" <wer...@yahoo.de> wrote in message
news:44febccd$0$18490$9b4e...@newsspool3.arcor-online.net...

--
Posted via a free Usenet account from http://www.teranews.com

pe...@nospam.demon.co.uk

unread,
Sep 6, 2006, 12:40:14 PM9/6/06
to
In article <44febccd$0$18490$9b4e...@newsspool3.arcor-online.net>
wer...@yahoo.de "Werner Sammer" writes:

Werner,

You might get some tidier/specific replies to alt.msdos.batch.nt
but since I'm reading this in a comp.os.msdos group I'll give you
a MSDOS solution (which should also work in XP).

mytest.bat:

@echo off
if %1.==[FLAG]. goto mysub

set parm1=hello
set parm2=12345
call %0 [FLAG] %parm1% %parm2%

set parm1=test
set parm2=7777
call %0 [FLAG] %parm1% %parm2%

...
goto END

:MYSUB %1 %2
Echo I got the following parameters: %1 and %2

:END
echo.

"[FLAG]" can be anything you like, just something that is likely
to never be the first real parameter to your batch file.

See if that does what you want.

Pete
--
"We have not inherited the earth from our ancestors,
we have borrowed it from our descendants."

billious

unread,
Sep 6, 2006, 1:26:36 PM9/6/06
to

"Werner Sammer" <wer...@yahoo.de> wrote in message
news:44febccd$0$18490$9b4e...@newsspool3.arcor-online.net...

mytest.bat:
-----------
set parm1=hello
set parm2=12345
CALL :mysub %parm1% %parm2%

set parm1=test
set parm2=7777
CALL :mysub %parm1% %parm2%
...
GOTO :EOF

:MYSUB


Echo I got the following parameters: %1 and %2

GOTO :EOF

Note:

CALL :label
the colon is required to specify "internal routine"

MYSUB should not have the %1, %2

%1 and %2 within the routine MYSUB are those passed through the CALL
statement

GOTO :EOF
(the colon is required) is defined in NT+ as "the end of the batch file" and
the label should not be defined within the batch.

(Note here : used to skip over the internal routine code and also at the end
of the routine. This second is merely good practise (IMHO) since it prevents
flow-through to any routine installed AFTER the (in this case) MYSUB
routine.)

Ted Davis

unread,
Sep 6, 2006, 2:27:18 PM9/6/06
to
On 06 Sep 2006 12:19:25 GMT, wer...@yahoo.de (Werner Sammer) wrote:

>Inside a DOS batch script (in WinXP) I want to call several times a function/procedure.
>This function/procedure should be in the same *.bat batch script as the calling statement
>and take some parameters.
>
>How can I implement such a function call (especially how do I work inside the function/procedure with
>the received parameter values) ?
>
>The following does NOT work (although I would appreciate to use it in a similar style):
>
>
>
>mytest.bat:
>
>set parm1=hello
>set parm2=12345
>goto mysub %parm1% %parm2%

Not "goto" - use call
call :mysub %parm1% %parm2%


>
>set parm1=test
>set parm2=7777
>goto mysub %parm1% %parm2%

Not "goto" - use call
call :mysub %parm1% %parm2%
>
>...
>
>exit
Replace "exit" with
goto :EOF


>
>:MYSUB %1 %2
>
>Echo I got the following parameters: %1 and %2
>
>goback

Remove "goback" and either put the sub last thing in the file, or
terminate it with
goto :EOF

(:EOF is always defined so you don't have to - it's the end of the
file)


--
T.E.D. (tda...@gearbox.maem.umr.edu) Remove "gearbox.maem" to get real address - that one is dead

Todd Vargo

unread,
Sep 6, 2006, 4:48:36 PM9/6/06
to

"Werner Sammer" <wer...@yahoo.de> wrote in message
news:44febccd$0$18490$9b4e...@newsspool3.arcor-online.net...

Although alt.msdos.batch.nt would be better place to ask XP related batch
questions, the key is to use CALL instead of GOTO. Type GOTO/? and CALL/? at
the prompt for details on usage and differences.

--
Todd Vargo
(Post questions to group only. Remove "z" to email personal messages)

Todd Vargo

unread,
Sep 6, 2006, 9:59:07 PM9/6/06
to

"Todd Vargo" <tlv...@sbcglobal.netz> wrote in message
news:EuGLg.8418$yO7....@newssvr14.news.prodigy.com...

>
> "Werner Sammer" <wer...@yahoo.de> wrote in message
> news:44febccd$0$18490$9b4e...@newsspool3.arcor-online.net...
snip...

> > NT extensions can be used.
>
> Although alt.msdos.batch.nt would be better place to ask XP related batch
> questions,

Sorry, crosspost and follow-up setting caught me off guard.

Tom Lavedas

unread,
Sep 7, 2006, 2:07:03 PM9/7/06
to
You need to CALL subroutines in batch, not GOTO them. Then you need to
provide flow control after the call to keep from going through them an
additional time (assuming they are coded below the calling line.

For example, ...

parm1=hello
set parm2=12345
call :mysub %parm1% %parm2%

set parm1=test
set parm2=7777
call :mysub %parm1% %parm2%
goto eof:

:MYSUB %1 %2
Echo I got the following parameters: %1 and %2

:: -------- FILE ENDS HERE ---------

Tom Lavedas
============
http://members.cox.net/tglbatch

The Joker

unread,
Sep 7, 2006, 9:04:05 PM9/7/06
to
On 06 Sep 2006 12:19:25 GMT, Werner Sammer wrote:

> Inside a DOS batch script (in WinXP) I want to call several times a function/procedure.
> This function/procedure should be in the same *.bat batch script as the calling statement
> and take some parameters.
>
> How can I implement such a function call (especially how do I work inside the function/procedure with
> the received parameter values) ?
>

Hi Werner,

how about adding a label into the batch file like Sub01 and then using CALL
to re-run the same batch file passing the parameters? You would need to
either also pass an additional parameter to indicate that the batch file
should jump to the required label. So...

if not (%GT%)==() GOTO %GT%
....
Set GT=Sub01
Call SameBat P1 P2 P3
REM Next required to handle return from Call
GOTO End
:Sub01
REM Sub to handle parms.

Instead of using Call you could simple re-run the batch once you have SET a
variable to ensure that it switches control to the correct routine.

Any good? Joker!

Timo Salmi

unread,
Sep 7, 2006, 11:51:11 PM9/7/06
to
Werner Sammer wrote:
> Inside a DOS batch script (in WinXP) I want to call several times
> a function/procedure.

You may wish to take a look at these recent postings in an other
thread which gives you "live" examples.

http://www.google.com/groups?selm=edosuc$rus$1%40saavi.uwasa.fi
http://www.google.com/groups?selm=edoqab$q3j$1%40saavi.uwasa.fi

All the best, Timo

--
Prof. Timo Salmi ftp & http://garbo.uwasa.fi/ archives 193.166.120.5
Department of Accounting and Business Finance ; University of Vaasa
mailto:t...@uwasa.fi <http://www.uwasa.fi/~ts/> ; FIN-65101, Finland
Useful script files and tricks ftp://garbo.uwasa.fi/pc/link/tscmd.zip

0 new messages