windows 检测批处理文件的执行方式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/377407/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Detecting how a batch file was executed
提问by Dan Olson
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.
假设是 Windows,有没有办法可以从批处理文件中检测它是从打开的命令提示符启动还是通过双击启动?我想在批处理结束时添加一个暂停,当且仅当它被双击时,这样窗口不会随着它可能产生的任何有用输出一起消失。
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.
有什么聪明的方法可以做到这一点?我正在寻找可以依赖的解决方案,以便在或多或少配置为默认设置的机器上工作。
采纳答案by Patrick Cuff
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.
- 从打开的命令提示符运行时, %0 变量的路径周围没有双引号。如果脚本驻留在当前目录中,则甚至不提供路径,只提供批处理文件名。
- 从资源管理器运行时,%0 变量始终用双引号括起来,并包含批处理文件的完整路径。
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
:
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.
这在从打开的命令提示符运行时会起作用,但是当从资源管理器运行时,它会抱怨该if
语句不正确。
回答by Ken Hymanson
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:
是的。Patrick Cuff 的最后一个例子几乎奏效了,但是您需要添加一个额外的转义符 '^',以使其在所有情况下都有效。这对我很有用:
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.
但是,如果批处理文件的名称包含空格,则无论哪种情况,它都会被双引号引起来,因此此解决方案将不起作用。
回答by hamishmcn
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
不要忽视有两个批处理文件的解决方案:
abatfile.bat 和 abatfile-with-pause.bat
第二个只是调用第一个并添加暂停
回答by npocmaka
Here's what I use :
这是我使用的:
rem if double clicked it will pause
for /f "tokens=2" %%# in ("%cmdcmdline%") do if /i "%%#" equ "/c" pause
回答by waheed
One easy way to do it is described here: http://steve-jansen.github.io/guides/windows-batch-scripting/part-10-advanced-tricks.htmlThere is little typo in the code mentioned in the link. Here is correct code:
这里描述了一种简单的方法:http: //steve-jansen.github.io/guides/windows-batch-scripting/part-10-advanced-tricks.html 链接中提到的代码几乎没有错字. 这是正确的代码:
@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
回答by Andrew J. Brehm
I use a parameter "automode" when I run my batch files from scripts.
当我从脚本运行批处理文件时,我使用参数“automode”。
set automode=%7
(Here automode is the seventh parameter given.)
(这里 automode 是给出的第七个参数。)
Some code follows and when the file should pause, I do this:
下面是一些代码,当文件应该暂停时,我这样做:
if @%automode%==@ pause
回答by foraidt
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.
这意味着只有一个批处理文件,但-nopause
在从控制台调用时必须指定一个参数或类似的东西。
回答by foraidt
crazy idea: use tasklist and parse it's results. I've wrote in a test batch file:
疯狂的想法:使用任务列表并解析它的结果。我在测试批处理文件中写道:
tasklist > test.out
任务列表> 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)
当我双击它时,在任务列表进程之前有一个额外的“cmd.exe”进程,当脚本从命令行运行时不存在(但请注意,如果有人打开命令,这可能还不够行外壳然后双击批处理文件)
回答by Learning
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 )
不管它是如何打开的,只需添加暂停?如果它是从命令提示符打开的,除了无害的暂停之外不会造成任何伤害。(不是解决方案,只是考虑暂停是否会如此有害/烦人)