暂停批处理文件时,双击而不是从一个控制台窗口中运行时? [英] Pausing a batch file when double-clicked but not when run from a console window?
问题描述
是否有一个批处理文件的方式(在这种情况下,运行在Windows XP),以确定它是否是从一个命令行(即控制台窗口内)发射或通过壳(例如通过双击)发起?
Is there a way for a batch file (in this case, running on Windows XP) to determine whether it was launched from a command line (i.e. inside a console window) or launched via the shell (e.g. by double-clicking)?
我有,我想在某些点时,通过shell中运行,而不是在一个命令行中运行有停顿了一下,一个脚本。我见过<一个href=\"http://stackoverflow.com/questions/886848/how-to-make-windows-batch-file-pause-when-double-clicked\">a在如此相似的问题,但我无法使用相同的解决方案有两个原因:第一,它是否暂停需要依赖于多种因素,其中只有一个是它是否是双击。其次,我会在我的球队进行发行这个剧本给别人,我不能切实让所有的人都做出更改注册表,将影响所有的脚本。
I have a script which I'd like to have pause at certain points when run via the shell, but not when run at a command line. I've seen a similar question on SO, but am unable to use the same solution for two reasons: first, whether or not it pauses needs to be dependent on multiple factors, only one of which is whether it was double-clicked. Second, I'll be distributing this script to others on my team and I can't realistically ask all of them to make registry changes which will affect all scripts.
这可能吗?
推荐答案
找到1位:-) - 后拼命想着什么 CMD
可能交互而不是在运行时做直接启动一个批处理文件...我终于发现之一。
Found one :-) – After desperately thinking of what cmd
might do when run interactively but not when launching a batch file directly ... I finally found one.
伪变量%CMDCMDLINE%
包含用于启动命令行 CMD
。如果 CMD
已正常启动这个包含一个类似于以下内容:
The pseudo-variable %cmdcmdline%
contains the command line that was used to launch cmd
. In case cmd
was started normally this contains something akin to the following:
"C:\Windows\System32\cmd.exe"
然而,启动一个批处理文件时,它看起来是这样的:
However, when launching a batch file it looks like this:
cmd /c ""C:\Users\Me\test.cmd" "
小演示:
@echo off
for %%x in (%cmdcmdline%) do if /i "%%~x"=="/c" set DOUBLECLICKED=1
if defined DOUBLECLICKED pause
检查的这种方式可能不是最强大的,虽然,但 / C
应该只有present仿佛一个批处理文件,直接展开了争论。
This way of checking might not be the most robust, though, but /c
should only be present as an argument if a batch file was launched directly.
在这里测试在Windows 7 64位系统。它可能会或可能无法正常工作,休息,做一些怪异,吃儿童(也许是一件好事),或咬你的鼻子。
Tested here on Windows 7 x64. It may or may not work, break, do something weird, eat children (might be a good thing) or bite you in the nose.
这篇关于暂停批处理文件时,双击而不是从一个控制台窗口中运行时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!