rsk82
rsk82

Reputation: 29377

how to determine if *.bat file is running from console or on its own?

Is there a way to check that so I can put a conditional at the end not to close console window or wait for user key press but omit that if *.bat file was typed from the keyboard in another console and there is no need to do that because windows wont close at the end ?

Upvotes: 4

Views: 506

Answers (1)

Gray
Gray

Reputation: 7130

You can use %CMDCMDLINE% to check.

@echo off
CALL :GETMYSWITCH %CMDCMDLINE%
IF /I "%MYSWITCH%" == "/C" ECHO Used explorer & PAUSE
IF /I NOT "%MYSWITCH%" == "/C" ECHO used cmd

:GETMYSWITCH
SET MYSWITCH=%2

If it is the case that your batch file is being called from a script that you wrote rather than directly through a double-click, you can just have your script pass an argument to the batch file, then check for that to see if you need to pause.

Updated my answer to use this 'function' that I got from here.

Upvotes: 3

Related Questions