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.
Tuesday, 18 October 2011 at 11:37 |
That helped me, thanks!