Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 199 Vote(s) - 3.64 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Detecting how a batch file was executed

#1
Assuming Windows, is there a way I can detect from within a batch file if it was launched from an open command prompt or by double-clicking? I'd like to add a pause to the end of the batch process if and only if it was double clicked, so that the window doesn't just disappear along with any useful output it may have produced.

Any clever ways to do this? I'm looking for solutions I could rely on to work on a machine that was configured more or less with default settings.
Reply

#2
Just add pause regardless of how it was opened? If it was opened from command prompt no harm done apart from a harmless pause. (Not a solution but just thinking whether a pause would be so harmful / annoying )
Reply

#3
Don't overlook the solution of having two batch files:
abatfile.bat and abatfile-with-pause.bat
The second simply calling the first and adding a pause
Reply

#4
Similar to a second batch file you could also pause if a certain parameter is not given (called via clicking).

This would mean only one batch file but having to specify a `-nopause` parameter or something like that when calling from the console.
Reply

#5
crazy idea: use tasklist and parse it's results.
I've wrote in a test batch file:

tasklist > test.out

and when I double-clicked it, there was an additional "cmd.exe" process just before the tasklist process, that wasn't there when the script was run from command line (but note that might not be enough if someone opens a command line shell and then double-click the batch file)
Reply

#6
I just ran a quick test and noticed the following, which may help you:

- When run from an open command prompt, the %0 variable does not have double quotes around the path. If the script resides in the current directory, the path isn't even given, just the batch file name.
- When run from explorer, the %0 variable is always enclosed in double quotes and includes the full path to the batch file.

This script will not pause if run from the command console, but will if double-clicked in Explorer:

@echo off
setlocal enableextensions

set SCRIPT=%0
set DQUOTE="

@echo do something...

@echo %SCRIPT:~0,1% | findstr /l %DQUOTE% > NUL
if %ERRORLEVEL% EQU 0 set PAUSE_ON_CLOSE=1

:EXIT
if defined PAUSE_ON_CLOSE pause

EDIT:
There was also some weird behavior when running from Explorer that I can't explain. Originally, rather than

@echo %SCRIPT:~0,1% | findstr /l %DQUOTE% > NUL
if %ERRORLEVEL% EQU 0 set PAUSE_ON_CLOSE=1

I tried using just an `if`:

if %SCRIPT:0,1% == ^" set PAUSE_ON_CLOSE=1

This would work when running from an open command prompt, but when run from Explorer it would complain that the `if` statement wasn't correct.
Reply

#7
I use a parameter "automode" when I run my batch files from scripts.

set automode=%7

(Here automode is the seventh parameter given.)

Some code follows and when the file should pause, I do this:

if @%automode%==@ pause

Reply

#8
Yes. Patrick Cuff's final example almost worked, but you need to add one extra escape, '^', to make it work in all cases. This works great for me:

set zero=%0
if [^%zero:~0,1%] == [^"] pause

However, if the name of the batch file contains a space, it'll be double quoted in either case, so this solution won't work.
Reply

#9
Here's what I use :

rem if double clicked it will pause
for /f "tokens=2" %%# in ("%cmdcmdline%") do if /i "%%#" equ "/c" pause
Reply

#10
One easy way to do it is described here:

[To see links please register here]

There is little typo in the code mentioned in the link. Here is correct code:

@ECHO OFF
SET interactive=0

ECHO %CMDCMDLINE% | FINDSTR /L /I %COMSPEC% >NUL 2>&1
IF %ERRORLEVEL%==0 SET interactive=1

ECHO do work

IF "%interactive%"==1 PAUSE
EXIT /B 0
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through