[英]Pausing a batch file when double-clicked but not when run from a console window?
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 (ie inside a console window) or launched via the shell (eg by double-clicking)? 批处理文件(在这种情况下,在Windows XP上运行)是否可以确定是从命令行启动(即在控制台窗口内)还是通过外壳启动(例如,双击)?
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.
我已经在SO上看到了类似的问题 ,但是由于两个原因而无法使用相同的解决方案:首先,它是否暂停需要取决于多个因素,其中只有一个是是否被双击。 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.
其次,我将把这个脚本分发给团队中的其他人,我无法现实地要求他们所有人进行注册表更改,而这将影响所有脚本。
Is this possible? 这可能吗?
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. 找到了一个:-) –拼命考虑了以交互方式运行时
cmd
会执行什么操作,而不是直接启动批处理文件时……我终于找到了一个。
The pseudo-variable %cmdcmdline%
contains the command line that was used to launch cmd
. 伪变量
%cmdcmdline%
包含用于启动cmd
的命令行。 In case cmd
was started normally this contains something akin to the following: 如果
cmd
正常启动,则其中包含类似于以下内容的内容:
"C:\Windows\System32\cmd.exe"
However, when launching a batch file it looks like this: 但是,启动批处理文件时,它看起来像这样:
cmd /c ""C:\Users\Me\test.cmd" "
Small demo: 小型演示:
@echo off
for %%x in (%cmdcmdline%) do if /i "%%~x"=="/c" set DOUBLECLICKED=1
if defined DOUBLECLICKED pause
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. 但是,这种检查方法可能不是最可靠的,但是
/c
仅当直接启动批处理文件时才应作为参数出现。
Tested here on Windows 7 x64. 在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.
它可能会或可能不会起作用,破坏,做一些奇怪的事情,吃孩子(可能是一件好事)或咬你的鼻子。
A consolidated answer, derived from much of the information found on this page (and some other stack overflow pages with similar questions). 一个合并的答案,该答案来自此页上的许多信息(以及其他存在类似问题的堆栈溢出页面)。 This one does not rely on detecting /c, but actually checks for the name of the script in the command line.
该脚本不依赖于检测/ c,而是实际上在命令行中检查脚本的名称。 As a result this solution will not pause if you double-clicked on another batch and then called this one;
因此,如果您双击另一批次然后调用该批次,则该解决方案将不会暂停。 you had to double-click on this particular batch file.
您必须双击该特定的批处理文件。
:pauseIfDoubleClicked
setlocal enabledelayedexpansion
set testl=%cmdcmdline:"=%
set testr=!testl:%~nx0=!
if not "%testl%" == "%testr%" pause
Edit: The same can be done in a single line: 编辑:可以在一行中完成相同操作:
echo %cmdcmdline% | findstr /i /c:"%~nx0" && set standalone=1
In plain English, this 用简单的英语来说
%cmdcmdline%
to findstr , which then searches for the current script name %cmdcmdline%
的值通过管道%cmdcmdline%
给findstr ,然后查找当前脚本名称 %0
contains the current script name, of course only if shift
has not been called beforehand %0
包含当前的脚本名称,当然,只有在未事先调用shift
情况下 %~nx0
extracts file name and extension from %0
%~nx0
从%0
提取文件名和扩展名 >NUL 2>&1
mutes findstr by redirecting any output to NUL >NUL 2>&1
通过将任何输出重定向到NUL来使findstr静音 &&
only executes if the preceding command returned without error &&
仅在前面的命令没有错误返回的情况下执行 standalone
will not be defined if the script was started from the command line standalone
Later in the script we can do: 在脚本的后面,我们可以做:
if defined standalone pause
One approach might be to create an autoexec.nt file in the root of c:\\ that looks something like: 一种方法可能是在c:\\的根目录中创建一个autoexec.nt文件,该文件类似于:
@set nested=%nested%Z
In your batch file, check if %nested% is "Z" - if it is "Z" then you've been double-clicked, so pause. 在您的批处理文件中,检查%nested%是否为“ Z”-如果为“ Z”,则已经双击了您,因此请暂停。 If it's not "Z" - its going to be "ZZ" or "ZZZ" etc as CMD inherits the environment block of the parent process.
如果不是“ Z”,则它将是“ ZZ”或“ ZZZ”等,因为CMD会继承父进程的环境块。
-Oisin -Oisin
A little more information... 更多信息...
I start with a batch-file (test.cmd) that contains: 我从包含以下内容的批处理文件(test.cmd)开始:
@echo %cmdcmdline%
If I double-click the "test.cmd" batch-file from within Windows Explorer, the display of echo %cmdcmdline% is: 如果我从Windows资源管理器中双击“ test.cmd”批处理文件,则回显%cmdcmdline%的显示为:
cmd /c ""D:\Path\test.cmd" "
When executing the "test.cmd" batch-file from within a Command Prompt window, the display of 在命令提示符窗口中执行“ test.cmd”批处理文件时,显示
echo %cmdcmdline% depends on how the command window was started... echo%cmdcmdline%取决于命令窗口的启动方式...
If I start "cmd.exe" by clicking the "Start-Orb" and "Command Prompt" or if I click "Start-Orb" and execute "cmd.exe" from the search/run box. 如果通过单击“启动球”和“命令提示符”启动“ cmd.exe”,或者单击“启动球”并从搜索/运行框中执行“ cmd.exe”。 Then I execute the "test.cmd" batch-file, the display of echo %cmdcmdline% is:
然后,我执行“ test.cmd”批处理文件,回显%cmdcmdline%的显示为:
"C:\Windows\system32\cmd.exe"
Also, for me, if I click "Command Prompt" from the desktop shortcut, then execute the "test.cmd" batch-file, the display of echo %cmdcmdline% is also: 另外,对于我来说,如果我从桌面快捷方式单击“命令提示符”,然后执行“ test.cmd”批处理文件,则回显%cmdcmdline%的显示也为:
"C:\Windows\system32\cmd.exe"
But, if I "Right-Click" inside a Windows Explorer window and select "Open Command Prompt Here", then execute the "test.cmd" batch-file, the display of echo %cmdcmdline% is: 但是,如果我在Windows资源管理器窗口中“右键单击”并选择“在此处打开命令提示符”,然后执行“ test.cmd”批处理文件,则回显%cmdcmdline%的显示为:
"C:\Windows\System32\cmd.exe" /k ver
So, just be careful, if you start "cmd.exe" from a shortcut that contains a "/c" in the "Target" field (unlikely), then the test in the previous example will fail to test this case properly. 因此,请小心,如果从“目标”字段中包含“ / c”的快捷方式(不太可能)启动“ cmd.exe”,则上一个示例中的测试将无法正确测试这种情况。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.