There are (at least) the two possibilities, one of which is courtesy of user jeb in this answer of him – so please give adequate credit to him by up-voting his post!
main.bat
, establishing two calls of label :Label
in sub.bat
:
@echo off
echo/
echo ^>^>^> Supply `:Label` as the first argument:
call "%~dp0sub.bat" :Label arg1 arg2 arg3
echo ^>^>^> Returned to main script at this point.
echo/
echo ^>^>^> Embed `:Label` within the script path:
call "%~d0\:Label:\..%~p0sub.bat" arg1 arg2 arg3
echo ^>^>^> Returned to main script at this point.
exit /B
sub.bat
, resolving label :Label
in two distinct ways:
@echo off
echo Original path: "%~0"
echo Resolved path: "%~f0"
echo 1st argument : "%~1"
echo All arguments: %*
rem // Check whether first argument begins with (a) colon(s):
for /F "tokens=* delims=:" %%L in ("%~1") do if not "%%~L"=="%~1" goto :%%~L
rem // Check whether script path contains something between colons behind the drive:
for /F "tokens=3 delims=:" %%L in ("%~0") do goto :%%~L
rem // This code in the main section is never reached when a label has been provided.
exit /B
:Label
echo Function call: "%~f0" %*
exit /B
And this is the console output upon running main.bat
:
>>> Supply `:Label` as the first argument:
Original path: "C:\LocalFiles\TiKi-ASIC\doc\work\TiCi-SV\Spec\sub.bat"
Resolved path: "C:\LocalFiles\TiKi-ASIC\doc\work\TiCi-SV\Spec\sub.bat"
1st argument : ":Label"
All arguments: :Label arg1 arg2 arg3
Function call: "C:\LocalFiles\TiKi-ASIC\doc\work\TiCi-SV\Spec\sub.bat" :Label arg1 arg2 arg3
>>> Returned to main script at this point.
>>> Embed `:Label` within the script path:
Original path: "C:\:Label:\..\LocalFiles\TiKi-ASIC\doc\work\TiCi-SV\Spec\sub.bat"
Resolved path: "C:\LocalFiles\TiKi-ASIC\doc\work\TiCi-SV\Spec\sub.bat"
1st argument : "arg1"
All arguments: arg1 arg2 arg3
Function call: "C:\LocalFiles\TiKi-ASIC\doc\work\TiCi-SV\Spec\sub.bat" arg1 arg2 arg3
>>> Returned to main script at this point.
As you may have noticed, in the first call, the label :Label
is also part of the argument string %*
which you have to pay specific attention to, though in the second call (applying said jeb's method), %*
contains the pure argument string without an extra item.