file1.bat:
#echo off
:Test
echo in file one
call file2.bat (Here i want to call only Demo routine in the file2.bat)
file2.bat:
:hello
echo in hello
:Demo
echo in Demo
From the batch file1 I want to make a call to a sub routine in the batch file2.
I tried for example call file2.bat:Demo but it didn't give the correct result.
How I can achieve this?
the file with subroutines must look like:
#echo off
call :%*
exit /b %errorlevel%
:hello
echo in hello
exit /b 0
:Demo
echo in Demo with argument %1
exit /b 0
then from the other file you can call it like
call file2.bat demo "arg-one"
You can write your functions file (in this sample it is library.cmd) as
#echo off
setlocal enableextensions
rem Not to be directly called
exit /b 9009
:test
echo test [%*]
goto :eof
:test2
echo test2 [%*]
goto :eof
:testErrorlevel
echo testErrorlevel
exit /b 1
And then the caller batch can be something like
#echo off
setlocal enableextensions disabledelayedexpansion
call :test arg1 arg2 arg3
call :test2 arg4 arg5 arg6
call :testErrorlevel && echo no errorlevel || echo errorlevel raised
goto :eof
:test
:test2
echo calling function %0
library.cmd %*
:testErrorlevel
echo calling function %0
library.cmd
In this case, the labels need to be defined with the same name in both files.
The direct invocation of the "library" batch file will replace the context of the call :label, and when the invoked batch is readed, a goto :label is internally executed and code continues inside the indicated label. When the called batch file ends, the context is released and the code after the call :label continues.
edited
As Jeb points in comments, there is a drawback in this method. The code running in the called batch file can not use %0 to retrieve the name of the function being called, it will return the name of the batch file. But if needed, the caller can do it as shown in the sample code.
edited 2016/12/27
Answering to dbenham, I have no way to know if it was a coding error or an intended feature, but this is how the process works
The lines in batch file are processed inside the inner BatLoop function when a batch "context" is created. This function receives, as one of its arguments, a pointer to the command that caused the "context" to be created.
Inside this function the commands in the batch file are iterated. The loop that iterates over the commands makes a test in each iteration: if extensions are enabled, it is the first line in the batch file and the arguments of the command that started the context starts with a colon (a label), a goto is generated to jump to the label.
Up to here, I have to suppose that this is the intended behaviour to handle the call :label syntax: create a new "context", load the file, jump to the label.
But the command argument received is never changed, a different variable is used to track the execution of the commands in the batch file. If a new batch file is loaded into / overwrites the current batch "context" (we have not used call command), after loading the new batch code, BatLoop resets the line count (we start at the first line of the loaded file) and, voila, the condition at the start of the loop (extensions enabled, first line, the colon) is true again (the pointed input command has not been changed) and a new goto is generated.
To comment a bit more, here is the code I came out reading the answer. It basically give you a 'utility' file where you can add your sub/fnc in a common library for code maintenance.
A) As a example, here is the 'utility_sub.cmd' file:
REM ==============================================
REM CALL THE SELECTED SUB
REM ==============================================
REM echo %~1
GOTO :%~1
REM ==============================================
REM ERROR MANAGEMENT
REM ==============================================
REM Ref : https://ss64.com/nt/exit.html
:RAISE_ERROR
EXIT /B 1
:RESET_ERROR
EXIT /B 0
REM Demo call
REM =========
REM CALL :RAISE_ERROR
REM echo RAISE_ERROR ERRORLEVEL = %ERRORLEVEL%
REM If %ERRORLEVEL% GTR 0 (
REM set msg="%tab%- Error detected ..."
REM CALL :SUB_STDOUT_MSG !msg!, 1
REM )
REM CALL :RESET_ERROR
REM echo RESET_ERROR ERRORLEVEL = %ERRORLEVEL%
REM ==============================================
REM SUB_STDOUT_MSG
REM ==============================================
:SUB_STDOUT_MSG
REM CALL :SUB_STDOUT_MSG "%param1%", %param2%, %param3%
REM Instead of this stdout sub, we can use Unix 'tee.exe'
REM but there is no 'line counter' feature like this sub
REM Call example :
REM EDI_Generate_Stat_Csv | tee c:\temp\voir.txt
REM Def :
REM Capture output from a program and also display the output to the screen, at the same time.
REM %~1 => Expand %1 removing any surrounding quotes (")
set msg=%~2
set sendtoLog=%3
set addCounter=%4
If !msg!==. (
REM Write empty line
echo!msg!
If !sendtoLog! EQU 1 (
echo!msg! >> %log_file%
)
) else (
REM (a) Write comment line (b) add counter if any
If !addCounter! EQU 1 (
set /a msgCounter+=1
set msg=!msgCounter! - !msg!
REM Pad counter left for single digit
If !msgCounter! LSS 10 (
set msg=0!msg!
)
)
REM Output to console
echo !msg!
REM Output to log
If !sendtoLog! EQU 1 (
echo !msg! >> %log_file%
)
)
EXIT /B
B) And here is how to call the 'SUB_STDOUT_MSG' in you 'main-logic' command file:
REM ... some other code here
REM ==============================================
REM PROGRAM END
REM ==============================================
set msg=.
CALL :SUB_STDOUT_MSG !msg!, 1
set msg="My programA - End"
CALL :SUB_STDOUT_MSG !msg!, 1
set msg="%date:~0,4%-%date:~5,2%-%date:~8,2% %time:~0,2%:%time:~3,2%:%time:~6,2%"
CALL :SUB_STDOUT_MSG !msg!, 1
set msg="+++++++++++++++"
CALL :SUB_STDOUT_MSG !msg!, 1
timeout 2 > Nul
REM Skip all SUB ROUTINE
GOTO :EOF
REM ==============================================
REM CALL SUB ROUTINE
REM ==============================================
:SUB_STDOUT_MSG
REM echo calling sub %0
CALL "C:\Utilitaires\Financement\Utility_Sub.cmd" SUB_STDOUT_MSG %*
EXIT /B
:EOF
What about providing the target label as the first agrument of the called script? You needed to modify the called dscript then though.
file1.bat (main):
#echo off
echo/
echo File "%~0": call "file2.bat" [no arguments]
call "file2.bat"
echo/
echo File "%~0": call "file2.bat" :DEMO
call "file2.bat" :DEMO
echo/
echo File "%~0": call "file2.bat" :DEMO A B C
call "file2.bat" :DEMO A B C
file2.bat (sub):
#echo off
set "ARG1=%~1" & if not defined ARG1 goto :TEST
if "%ARG1:~,1%"==":" goto %ARG1%
:TEST
echo File "%~nx0", :TEST; arguments: %*
goto :EOF
:DEMO
echo File "%~nx0", :DEMO; arguments: %*
echo before `shift /1`:
echo "%%~0" refers to "%~0"
echo "%%~1" refers to "%~1"
shift /1
echo after `shift /1`:
echo "%%~0" refers to "%~0"
echo "%%~1" refers to "%~1"
goto :EOF
Output:
>>> file1.bat
File "file1.bat": call "file2.bat" [no arguments]
File "file2.bat", :TEST; arguments:
File "file1.bat": call "file2.bat" :DEMO
File "file2.bat", :DEMO; arguments: :DEMO
before `shift /1`:
"%~0" refers to "file2.bat"
"%~1" refers to ":DEMO"
after `shift /1`:
"%~0" refers to "file2.bat"
"%~1" refers to ""
File "file1.bat": call "file2.bat" :DEMO A B C
File "file2.bat", :DEMO; arguments: :DEMO A B C
before `shift /1`:
"%~0" refers to "file2.bat"
"%~1" refers to ":DEMO"
after `shift /1`:
"%~0" refers to "file2.bat"
"%~1" refers to "A"
Related
I have a Script with different subroutines:
REM ---------------MAIN------------------------START----------------------------
call :SUB_GetStartTime
call :SUB_SettingVariables
call :SUB_CheckingParameters %*
call :SUB_Copy
call :SUB_GetEndTime
call :SUB_WriteLog
call :SUB_EndScreen
REM ---------------MAIN------------------------END------------------------------
At SUB_CheckingParameters I have this if query:
if "%~1"=="/help" (
GOTO SUB_HELP
)
If I pass the parameter /help it goes to my help window:
cls
ECHO ===================HELP==============
ECHO help text help text help text
ECHO =====================================
timeout /t 120
exit /b
after exit /b I want the script to end but it just goes to my next subroutine (SUB_Copy). Shouldnt the script end because I use GOTO SUB_Help and not call ?
Can someone help me and tell me what I am doing wrong?
I ususally handle this by passing back an errorlevel:
#Echo off
REM ---------------MAIN------------------------START----------------------------
call :SUB_GetStartTime
call :SUB_SettingVariables
call :SUB_CheckingParameters %* || Exit /b 1
call :SUB_Copy
call :SUB_GetEndTime
call :SUB_WriteLog
call :SUB_EndScreen
REM ---------------MAIN------------------------END------------------------------
Echo end of main
Pause
Goto :Eof
:SUB_CheckingParameters
if /I "%~1"=="/help" GOTO SUB_HELP
:SUB_GetStartTime
:SUB_SettingVariables
:SUB_Copy
:SUB_GetEndTime
:SUB_WriteLog
:SUB_EndScreen
Echo:We are in %~0 Args %*
Goto :Eof
:SUB_HELP
rem cls
ECHO ===================HELP==============
ECHO help text help text help text
ECHO =====================================
timeout /t 120
exit /b 1
If I want to call :foo in foo.bat from bar.bat I do:
::foo.bat
echo.wont be executed
exit /b 1
:foo
echo foo from foo.bat
exit /b 0
and
::bar.bat
call :foo
exit /b %errorlevel%
:foo
foo.bat
echo.will also not be executed
But if I don't know the label name but get it passed as a parameter I'm stuck
::bar.bat
:: calling a dynamic label is no problem
call :%~1
exit /b %errorlevel%
::don't know how to "catch-all" or set context of "current-label"
:%~1
foo.bat
You can use a batch parser trick.
You don't need to do anything in foo.bat for this to work
::foo.bat
echo.wont be executed
exit /b 1
:func1
echo foo from foo.bat
exit /b 0
:unknown
echo Hello from %0 Func :unknown
exit /b
You only need to add any labels into bar.bat that you want to call in foo.bat
#echo off
::bar.bat
call :unknown
echo Back from Foo
call :func1
echo Back from Foo
exit /b %errorlevel%
:unknown
:func1
foo.bat
echo NEVER COMES BACK HERE
The trick is that after calling a label in bar.bat and then start foo.bat without calling it (only foo.bat), the foo.bat is loaded and the last called label is jumped to.
Labels are searched by the parser literally before resolving environment variables or argument references, so you cannot use such in labels.
However, hereby I want to provide an approach that allows to use dynamic labels, although I do not understand what is the purpose of that, so this is more kind of an academic answer...
The batch file parser of cmd does not cache a batch file, it reads and executes it line by line, or correctly spoken, it reads and executes each command line/block individually. So we can make use of that and let the batch file modify itself during its execution, given that the modified part lies beyond the currently executed code portion. The following script accomplishes that:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Check whether a label name has been delivered:
if "%~1"=="" (
echo ERROR: No label name specified! 1>&2
exit /B 1
)
rem /* Call sub-routine to replace the literal label string `:%~1`
rem within this batch file by the given dynamic label name: */
call :REPLACE_LINE "%~f0" ":%%%%~1" ":%~1" || (
rem /* In case the given label is `:REPLACE_TEXT`, display error
rem message, clean up temporary file and quit script: */
>&2 echo ERROR: Label ":%~1" is already defined!
2> nul del "%~f0.tmp"
exit /B 1
)
rem // Perform call of the sub-routine with the dynamic label name:
call :%~1
rem /* Call sub-routine to replace the given dynamic label name
rem within this batch file by the literal label string `:%~1`: */
call :REPLACE_LINE "%~f0" ":%~1" ":%%%%~1"
endlocal
exit /B
:REPLACE_LINE val_file_path val_line_LOLD val_line_LNEW
::This sub-routine searches a file for a certain line
::case-insensitively and replaces it by another line.
::ARGUMENTS:
:: val_file_path path to the file;
:: val_line_LOLD line to search for;
:: val_line_LNEW line to replace the found line;
setlocal DisableDelayedExpansion
rem // Store provided arguments:
set "FILE=%~1" & rem // (path of the file to replace lines)
set "LOLD=%~2" & rem // (line string to search for)
set "LNEW=%~3" & rem // (line string to replace the found line)
set "LLOC=%~0" & rem // (label of this sub-routine)
rem // Write output to temporary file:
> "%FILE%.tmp" (
rem /* Read the file line by line; precede each line by a
rem line number and `:`, so empty lines do not appear as
rem empty to `for /F`, as this would ignore them: */
for /F "delims=" %%L in ('findstr /N "^" "%FILE%"') do (
rem // Store current line with the line number prefix:
set "LINE=%%L"
setlocal EnableDelayedExpansion
rem // Check current line against search string:
if /I "!LINE:*:=!"=="!LOLD!" (
rem // Current line equals search string, so replace:
echo(!LNEW!
) else if /I not "!LNEW!"=="!LLOC!" (
rem // Current line is different, so keep it:
echo(!LINE:*:=!
) else (
rem /* Current line equals label of this sub-routine,
rem so terminate this and return with error: */
exit /B 1
)
endlocal
)
)
rem /* Searching and replacement finished, so move temporary file
rem onto original one, thus overwriting it: */
> nul move /Y "%FILE%.tmp" "%FILE%"
endlocal
exit /B
:%~1
::This is the sub-routine with the dynamic label.
::Note that it must be placed after all the other code!
echo Sub-routine.
exit /B
Basically, it first replaces the literal label string (line) :%~1 by the string provided as the first command line argument, then it calls that section by call :%~1, and finally, it restores the original literal label string. The replacement is managed by the sub-routine :REPLACE_LINE.
foo.bat (secondary):
#echo off
echo this is foo.bat
REM check, if the label is defined in this script:
findstr /xi "%~1" %~f0 >nul 2>&1 || goto :error
goto %~1
:foo
echo reached foo.bat, label :foo
exit /b 0
:error
echo wrong or missing label: "%~1"
exit /b 1
bar.bat (primary)
#echo off
echo this is bar.bat
call foo.bat :foo
echo back to bar.bat - %errorlevel%
call foo.bat :foe
echo back to bar.bat - %errorlevel%
call foo.bat
echo back to bar.bat - %errorlevel%
exit /b
As aschipfl already correctly stated you can't have a dynamic label, but you can dynamically call present labels, but you should check for the presence prior calling it like Stephan does but in the primary batch.
So this is a combination of Stephans and jebs batches.
:: bar.bat
#echo off
REM check, if the label is defined in this script:
If "%~1" neq "" findstr /xi "%~1" %~f0 >nul 2>&1||goto :error&&Call :%~1
call :Func1
echo Back from Foo
call :func2
echo Back from Foo
exit /b %errorlevel%
:error
echo wrong or missing label: "%~1"
exit /b 1
:func1
:func2
foo.bat
echo NEVER COMES BACK HERE
:: foo.bat
#Goto :Eof
:func1
echo reached foo.bat, label :func1
exit /b 0
:func2
echo reached foo.bat, label :func2
exit /b 0
Sample output:
> bar
reached foo.bat, label :func1
Back from Foo
reached foo.bat, label :func2
Back from Foo
> bar fx
wrong or missing label: "fx"
I have written a batch file that runs fine under Windows Command Prompt, but I would like to be able to run it after POST in DOS. I have copied my code to AUTOEXEC.BAT file which gets executed automatically; however it comes up with syntax errors once it reaches the call command and the rest.
echo. This script is counting the # of POSTs.
echo. The POST # value is saved in TEST.txt.
echo.
call:myPOSTTest
for /f "tokens=* delims=" %%x in (A:\TEST.txt) do echo POST# %%x
echo. &pause&goto:eof
::--------------------------------------------------------
::-- Function section starts below here
::--------------------------------------------------------
:myPOSTTest - here starts my function identified by its label
set var=0
if EXIST A:\TEST.txt (
for /f %%x in (A:\TEST.txt) do (set /a var=%%x+1)
)
echo %var% >> A:\TEST.txt
goto END
:END
Thank You
See below for comments:
echo. This script is counting the # of POSTs.
echo. The POST # value is saved in TEST.txt.
echo.
call:myPOSTTest
MSDOS doesn't support the call :label syntax
for /f "tokens=* delims=" %%x in (A:\TEST.txt) do echo POST# %%x
MSDOS doesn't support the extended for commands
echo. &pause&goto:eof
MSDOS doesn't support the & command separator or the goto :eof link
::--------------------------------------------------------
::-- Function section starts below here
::--------------------------------------------------------
:myPOSTTest - here starts my function identified by its label
set var=0
if EXIST A:\TEST.txt (
for /f %%x in (A:\TEST.txt) do (set /a var=%%x+1)
)
MSDOS doesn't support the compound expressions in parentheses or the set /a enhancement
echo %var% >> A:\TEST.txt
goto END
:END
A little bit late, but ...
The call :function syntax can be substituted by an additional file for each function or by trampoline code on the batch file entry.
#echo off
for %%P in (/%1.) do if %%P==: goto %1
echo Program started
call %0 :func1
call %0 :func2 With :args
goto :eof
:func1
echo This is func1 with args: %1, %2, %3
goto :eof
:func2
echo This is func2 with args: %1, %2, %3
goto :eof
:eof
For the arithmetic part in set /a var=%%x+1 you can use an inc.bat or add.bat.
Even reading and processing lines from a text file is possible with old DOS.
In a subroutine, %0 expands to the subroutine name, not the script name. Is there a ligitimate way to still access the script name, or should I pass it as an argument?
#echo off
call :subroutine %~f0 my parameters
exit /b
:subroutine
shift
echo Script name is %0
echo Parameters: %1 %2
exit /b
I want the call statement to be just
call :subroutine my parameters
In a function you need to add at least one modifier to %~0.
call :test
exit /b
:test
echo 0 %0 - shows "test"
echo ~0 %~0 - shows "test"
echo ~f0 %~f0 - shows the batch name (full path)
exit /b
I believe %~nx0 will give you filename with extension and %~n0 will give you just the file name...
I prefer using this at the top of my scripts:
set "This=%~dpnx0"
This way you still keep the full path of the currently running script. If there's the need to get just the name of the script you can use a FOR /F loop to extract it:
set "This=%~dpnx0"
echo This=%This%
for /F %%I in ('echo.%This%') do set "Name=%%~nxI"
echo Name=!Name!
I have first.bat and second.bat.
first.bat is: call second.bat
Second is: echo %~n0 (displays filename of the executing batch)
The output is Second.bat, but I want it to display the caller filename, not it's own.
Is this possible?
This batch detects the name of the caller script or even if it's called directly from the command line
#echo off
setlocal DisableDelayedExpansion
set "func=%~0"
for /F "delims=\" %%X in ("%func:*\=%") do set "func=%%X"
if ":" == "%func:~0,1%" (
goto %func%
)
REM *** Get the name of the caller
(
(goto) 2>nul
setlocal DisableDelayedExpansion
call set "caller=%%~f0"
call set _caller=%%caller:*%%~f0=%%
if defined _caller (
set "callType=batch"
call "%~d0\:mainFunc\..%~pnx0" %*
) ELSE (
set "callType=cmd-line"
cmd /c "call "%~d0\:mainFunc\..%~pnx0" %*"
)
endlocal
)
echo NEVER REACHED
exit /b
:mainFunc
echo :mainFunc of %~nx0 arg1=%1 is called from '%caller%'/%callType%
exit /b
It uses the fact, that a (goto) statement will remove one level from the stack.
This results into leaving the current batch file and %~f0 will be the name of the caller script (and %~0 the current function of that batch) or the text %~f0 in the case of called from the command line.
Then the own script is called again with "%~d0\:mainFunc\..%~pnx0"
External Script
For easy use you could add a helper batch file.
Use it in your own scripts with this line
#echo off
<:GetCaller <nul call GetCaller.bat myCallerVar
echo This batch was called from "%myCallerVar%"
Name the helper batch file GetCaller.bat
#echo off
setlocal DisableDelayedExpansion
set "func=%~0"
for /F "delims=\" %%X in ("%func:*\=%") do set "func=%%X"
if ":" == "%func:~0,1%" (
goto %func%
)
REM *** STEP1
REM *** Get the filename of the caller of this script, needed for later restart that
(
(goto) 2>nul
setlocal DisableDelayedExpansion %= it could be reenabled by the GOTO =%
set "_returnVar=%~1"
call set "_lastCaller=%%~f0"
call set "_argToLastCaller=%%*"
call "%~d0\:Step2\..%~pnx0" %*
)
exit /b %= This is never reached =%
:Step2
REM *** STEP2
REM *** Get the filename/cmd-line of the caller of the script
(
(goto) 2>nul
(goto) 2>nul
setlocal DisableDelayedExpansion %= it could be reenabled by the GOTO =%
set "_returnVar=%_returnVar%"
set "_lastCaller=%_lastCaller%"
set "_argToLastCaller=%_argToLastCaller%"
call set "caller=%%~f0"
call set _caller=%%caller:*%%~f0=%%
if defined _caller (
set "callType=batch"
call "%~d0\:Step3batch\..%~pnx0"
) ELSE (
set "callType=cmd-line"
cmd /c "call "%~d0\:Step3batch\..%~pnx0" "
)
endlocal
)
exit /b %= This is never reached =%
:Step3batch
REM *** STEP3 Restart the requester batch, but jump to the label :GetCaller
call :GetCaller
exit /b %= This is never reached =%
:GetCaller
REM *** This uses the trick, that starting a batch without CALL will jump to the last used label
if "%_returnVar%" NEQ "" set "%_returnVar%=%_caller%"
%_lastCaller% %_argToLastCaller%
echo #6 never reached
I think the easiest way of doing this would be to pass the filename of the first batch as a parameter to the second, like this.
REM First.bat
call Second.bat %~n0
REM Second.bat
echo %1
Hope this helps!
Store the value of %~n0 in a environment variable before calling second.bat