简体   繁体   English

双击时暂停批处理文件,但从控制台窗口运行时不暂停?

[英]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
  1. The variable "testl" gets the full line of the cmd processor call, stripping out all of the pesky double quotes. 变量“ testl”获取cmd处理器调用的完整行,除去所有讨厌的双引号。
  2. The variable "testr" takes "testl" and further strips outs the name of the current batch file name if present (which it will be if the batch file was invoked with a double-click). 变量“ testr”为“ testl”,并进一步去除当前批处理文件名称(如果存在)(如果双击双击该批处理文件)。
  3. The if statement sees if "testl" and "testr" are different. if语句查看“ testl”和“ testr”是否不同。 If yes, batch was double-clicked, so pause; 如果是,则双击批处理,因此暂停; if no, batch was typed in on command line (or called from another batch file), go on. 如果否,则在命令行中键入批处理(或从另一个批处理文件调用),请继续。

Edit: The same can be done in a single line: 编辑:可以在一行中完成相同操作:

echo %cmdcmdline% | findstr /i /c:"%~nx0" && set standalone=1

In plain English, this 用简单的英语来说

  • pipes the value of %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静音
  • findstr sets a non-zero errorlevel if it can't find the substring in question 如果找不到相关的子字符串,则findstr设置一个非零的错误级别
  • && only executes if the preceding command returned without error &&仅在前面的命令没有错误返回的情况下执行
  • as a consequence, 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.

相关问题 双击时如何使Windows批处理文件暂停? - How to make windows batch file pause when double-clicked? 是否可以构建一个双击时不显示控制台窗口的控制台应用程序? - Is it possible to build a Console app that does not display a console Window when double-clicked? 在 Window OS 中双击时如何在空闲 shell 中运行脚本? - How to run a script in Idle shell when double-clicked in Window OS? 批处理文件在命令提示符下运行正常,但双击时给出错误 - Batch file runs fine in command prompt but gives error when double-clicked 双击时批处理文件闪烁并关闭 - Batch file flashes and closes when double Clicked 双击时在控制台中运行可执行的 JAR - Make an executeble JAR run in a console when double clicked 执行双击的Java jar文件时的输出流 - Output stream while executing a double-clicked java jar file 当我从Task Scheduler的bat文件中运行Java程序时,程序包不存在错误…但是当双击bat文件时它将运行 - package does not exist error when I run java program in a bat file from Task Scheduler… But It will run when a bat file double clicked 使用批处理文件启动java程序时隐藏控制台窗口? - hide the console window when use batch file to launch a java program? 计划任务调用的批处理文件在计划时抛出错误,双击运行良好 - Batch file called by scheduled task throws error when scheduled, runs fine when double clicked
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM