The problems is that CALL
command doubles the caret sign ^
and make double percentage a single one. And in hybrid files this can be a big problem.Or if a bat is not executed with CALL
from another bat and you want to warn the user.
The %cmdcmdline%
(despite this can be used if the file is executed directly from the prompt) and (goto)>nul
(eventually this can be used with this) techniques I think are not useful here.
I'm still thinking over this , but if anybody comes with elegant solution will be great.
EDIT. one possible approach is to detect the batch recursion level though a reliable way to check this configuration entry is needed (I have no idea how it is calculated).
The call command enables a user to execute a batch file from within another batch file.
By default, a batch file will display its command as it runs. The purpose of this first command which @echo off is to turn off this display. The command "echo off" turns off the display for the whole script, except for the "echo off" command itself. The "at" sign "@" in front makes the command apply to itself as well.
It can be done with some nasty tricks, so this isn't a perfect solution.
I count how many calls are necessary to get a stackoverflow and then I compare the value with the value from the error output.
But it's necessary to start a second cmd.exe process, as the stackoverflow kills the first batch instance
@echo off
setlocal EnableDelayedExpansion
set "_fileCheck=%~0"
if "!_fileCheck:~-4!"==".bAt" goto :child
del stackoverflow.log
start "" /b "%~dpn0.bAt" AllParameters
set count=0
(call :recusion & echo NEVER) 1> stackoverflow.log 2>&1
echo #* NEVER
exit /b
:recusion
set /a count+=1
echo +%count%
call :recusion
:child
ping -n 2 localhost 2>&1 > nul
:loop
(
< stackoverflow.log (
rem dummy
) || goto :loop
) 2>nul
:X
for /F "tokens=1,2 delims=,=" %%L in ('type stackoverflow.log') do (
set "line=%%L"
set "firstChar=!line:~0,1!"
if "!firstChar!"=="+" (
set /a maxCount=!line!
) ELSE if "!firstChar!" NEQ "*" (
set /a overflow=%%M
)
)
if !maxCount! EQU !overflow! (
echo Direct started
) ELSE (
echo CALL started
)
exit
As I understand your question, you want a method that allows a file.bat
file to determine if it was called via a call
command executed in other Batch file or not, and the way to do this is inserting additional checking code in the Batch file, right?
Well, a very simple method to do this is just inserting an additional line before the call command in the caller code in order to define a "call flag" variable. This way, when such variable is not defined in the file.bat
, then it was not called via call
:
In the caller code:
set "callFlag=1"
call file.bat
In the file.bat:
if not defined callFlag echo Warning, I was not executed via CALL command!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With