Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to detect if a batch file is started with CALL command or without?

Tags:

batch-file

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).

like image 538
npocmaka Avatar asked Nov 26 '15 13:11

npocmaka


People also ask

What is call command in batch file?

The call command enables a user to execute a batch file from within another batch file.

Do batch files always START with @echo off?

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.


2 Answers

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 
like image 82
jeb Avatar answered Sep 28 '22 01:09

jeb


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!
like image 33
Aacini Avatar answered Sep 28 '22 02:09

Aacini