MS-DOS Batch Tip: Function Calls, Parameters and Filenames

Here’s something I discovered by accident whilst using features I learned from the excellent site SS64.com site whilst struggling with the limitations of Batch.

Suppose you define a “function” (subroutine) in a Batch file. When you call that function, %0 contains the call label. But %nx0 contains the file name, so that need not be passed separately.

Here’s an example script:

@echo off

REM ***********************************
REM *** Main

call :Func "1st arg" "2nd arg"

goto :EOF

REM ***********************************
REM *** Function Definitions

:Func
echo params: "%~0" "%~1" "%~2"
echo calling file: "%~nx0"
echo full: "%~dpnx0%0"
goto :EOF
REM *** End :Func

and here’s some example output:

params: ":Func" "1st arg" "2nd arg"
calling file: "call-fn-param-test.bat"
full: "C:\code\shell\ms-dos\call-parameter-test.bat:Func"

For more information, see Simon Sheppard’s excellent site SS64.com containing pages on Bash scripting (for both Linux and Cygwin), MS-DOS batch scripting and in particular the page on MS-DOS batch parameters.

About these ads

One Response to “MS-DOS Batch Tip: Function Calls, Parameters and Filenames”

  1. vitalyb Says:

    That helped me, thanks!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.

%d bloggers like this: