Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling a batch script with LABEL from another batch script without CALL

Tags:

batch-file

cmd

I found an interesting feature of a cmd batch script. The question is: Is it a documented feature, or a bug? (You know, an undocumented unexpected feature is a bug :-) )

For start, I ensure you that I understand the distinction between calling a script using CALL and without CALL:

Script.bat
call Script.bat

Now, I have a batch library tools.bat:

echo tools.bat ARGS: %1 %2 %3 %4
set LABEL=%1
shift /1
goto %LABEL%

:A
echo A ARGS: %1 %2 %3
goto :eof
:B
echo B ARGS: %1 %2 %3
goto :eof

I call it from another script:

@echo off
call :A 1 2 3
call :B 4 5 6
exit /b

:A
:B
tools.bat %*

The strange, but potentially useful, behaviour is that the last line jumps directly to the label in tools.bat, not to the beginning of tools.bat. When I replace the last line with a CALL, I would have to rewrite the script substantially, because with CALL this unexpected behaviour does not work:

@echo off
call :A 1 2 3
call :B 4 5 6
exit /b

:A
call tools.bat :A %*
goto :eof
:B
call tools.bat :B %*
goto :eof

So again, is this feature documented or not? It works both in WIn7 and Win10.

like image 818
xarx Avatar asked Oct 19 '25 12:10

xarx


1 Answers

This is a known 'hack' but not documented by microsoft. One of the possible usages is a creating a 'libary' script that where you can call a specific function -> https://stackoverflow.com/a/30170342/388389

like image 65
npocmaka Avatar answered Oct 22 '25 05:10

npocmaka