You are not logged in.

#1 11 Feb 2010 09:31

Drewfus
Member
From: Australia
Registered: 11 Jan 2010
Posts: 44

Pause script run from Explorer, but not command prompt (before exit)

To prevent a script from immediately exiting when started from Explorer (double-clicked), add this line to the end of your scripts;

wmic process where "name='cmd.exe' or name='tcc.exe'" get commandline | find "%~dpnx0" > nul && (
(echo.& set /P var=Press a key to exit...))

If you need to pass the correct return code (errorlevel) to a calling command, add this section to the end of your script;

:End
set exitCode=%errorlevel%
wmic process where "name='cmd.exe' or name='tcc.exe'" get commandline | find "%~dpnx0" > nul && (
(echo.& set /P var=Press a key to exit...))
exit /B %exitCode%

Let me know if it works for you!

tcc.exe: http://www.jpsoft.com/tccledes.htm

Offline

#2 11 Feb 2010 12:29

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 1,129

Re: Pause script run from Explorer, but not command prompt (before exit)

Interesting method, but I think you've over-engineered it.

(if /i "%cmdcmdline:"=%" NEQ "%comspec% " pause) & (exit /b %errorlevel%)

This method works on (at least) XP, 2000, 2003, 2008.

Last edited by bluesxman (11 Feb 2010 13:09)


cmd | *sh | ruby | chef

Offline

#3 13 Feb 2010 16:55

Drewfus
Member
From: Australia
Registered: 11 Jan 2010
Posts: 44

Re: Pause script run from Explorer, but not command prompt (before exit)

Yes your right, but only this much is required;

> if /i "%cmdcmdline:"=%" NEQ "%comspec% " pause

In my method, executing wmic.exe was potentially changing the errorlevel just before the script exited, so i had to save it as an envar and return it using exit /b <exitcode>.
Your 'exit /b %errorlevel%' is redundant i believe, but whatever. Good one.

Offline

#4 14 Feb 2010 22:41

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 1,129

Re: Pause script run from Explorer, but not command prompt (before exit)

Drewfus wrote:

Your 'exit /b %errorlevel%' is redundant i believe

Good point.  I must confess that if I'm passing errorlevels out of a script it's typically one I've set myself, so I tend to forget that the last still stands!


cmd | *sh | ruby | chef

Offline

Board footer

Powered by