Jacob Aulin
Jacob Aulin

Reputation: 54

How to get a batch file only processed if it's called from another batch file?

I am coding a batch file and it needs some more files. But they files should only be able to run using the call function from another batch file. My code looks like this:

call compileData.bat
pause

I want the compilerData.bat just starts when it's called from this one, not if its just started from Explorer or something other.

Can you please help me?

I have tried to find a solution on this problem in a whole hour!

Upvotes: 1

Views: 1163

Answers (6)

oldGrey
oldGrey

Reputation: 1

Here's my solution:

when launched from the command line, %cmdcmdline% inherits the name from the base calling program, so it wouldn't be the name of the "middle man" calling your batch file

this is what I came up with. I had to use the "subroutine" method to get the variables properly expanded

Note: Edge Case: if you use complex paths with the batch files having the same name in different folders, you could run into an "Edge Case". If that is important to you, then you might have to further parse the file names. I'm not totally sure, it wasn't my use case so I didn't go further.

@echo OFF
setlocal EnableDelayedExpansion

call :myGetFileName "%CmdCmdLine%"
if /I "%sRet%"=="%~nx0" (
echo ************** Pause
) else (
echo ************** NO Pause
)

echo finished test
pause
exit

:myGetFileName
set "sRet=%~nx1"
exit /b

    

Upvotes: 0

Mofi
Mofi

Reputation: 49127

My suggestion is putting all your batch code into a single batch file and use subroutines. Open a command prompt window and run call /? for help on how to use subroutines which is nothing else than calling a batch file being embedded in current batch file.

A simple example:

@echo off
echo Running %~f0 %*
call :compileData %*
call :WaitForUser
rem The next line results in exiting processing of this batch file
goto :EOF

:compileData
echo/
echo Running subroutine compileData with the arguments: %*
rem Exit processing subroutine compileData and continue above
rem after the command line calling the subroutine compileData.
goto :EOF

:WaitForUser
echo/
pause
rem Exit processing subroutine WaitForUser and continue above
rem after the command line calling the subroutine WaitForUser.
goto :EOF

See also Where does GOTO :EOF return to? And take a look on DosTips forum topic ECHO. FAILS to give text or blank line - Instead use ECHO/ for the explanation on using echo/ to output an empty line.

Upvotes: 0

Squashman
Squashman

Reputation: 14320

You can use a not so well known system variable named cmdcmdline.

I will explain a brief usage for you.

For brevity's sake we will have two very simple batch files.

Parent.bat

@echo off
call compiledata.bat

And compiledata.bat

@echo off
echo %cmdcmdline%
pause

When compiledata.bat is executed on its own this variable's value is the batch file itself.

C:\WINDOWS\system32\cmd.exe /c ""C:\Batch\CALL\compiledata.bat" "

But when compiledata.bat is called from parent.bat the variable's value is that of the calling parent.bat.

C:\WINDOWS\system32\cmd.exe /c ""C:\Batch\CALL\parent.bat" "

Upvotes: 0

jwdonahue
jwdonahue

Reputation: 6669

Set an environment variable in the parent script, then if that variable is not set or doesn't have the correct value in the children, they just exit with an error message explaining they aren't intended for standalone use. You really can't prevent someone from reverse engineering the code and forcing it to run.

You could put the children in a password protected zip file and have the parent unpack it just before calling them. Then when the parent is done, it deletes the unpacked scripts.

Do all of the above.

Upvotes: 0

Harry
Harry

Reputation: 1263

I cannot think of any way that would prevent the bare "run" of the called script. Possibly that might only be done using NTFS permissions.

What you can do quickly is something like this:

MOTHERBATCH.bat

call compileData.bat SomePASSPHRASE

compileData.bat

@echo off
if not "%1"=="SomePASSPHRASE" (
echo "You can not run this script directly, please run MOTHERSCRIPT.bat."
exit /B 1
)

echo "Passphrase is correct, code is executed..."

Upvotes: 0

Stephan
Stephan

Reputation: 56198

You can use a parameter.

compileData.bat:

if "%1" neq "somestring" exit /b
REM rest of your code

Another.bat:

call compileData.bat somestring
pause

Upvotes: 2

Related Questions