简体   繁体   English

双击时如何使Windows批处理文件暂停?

[英]How to make windows batch file pause when double-clicked?

I've written a batch file to automate some tasks. 我写了一个批处理文件来自动执行某些任务。 I can run it from a command window and it runs and displays results. 我可以从命令窗口运行它,它运行并显示结果。 If I double click it from explorer though, it runs and terminates immediately so I can't see the results. 如果我从资源管理器中双击它,它会立即运行并终止,所以我看不到结果。

Is there a way I can make batch file window stay open until I dismiss it if I started it by double-clicking the icon? 有没有办法让批处理文件窗口保持打开状态,直到我解雇它,如果我通过双击图标启动它?

I don't want to have to pass a /nopause parameter or something when I call a batch file from the command line. 当我从命令行调用批处理文件时,我不想传递/ nopause参数。 I'd like a solution where I can use the batch file without having to do anything too special? 我想要一个解决方案,我可以使用批处理文件,而不必做任何太特别的事情?

Thanks. 谢谢。

NOTE I don't want it to pause when running from the command line!! 注意我不希望它从命令行运行时暂停!! I may call this batch file from another batch file to carry out a load of operations. 我可以从另一个批处理文件中调用此批处理文件来执行一系列操作。 In that case I can't be sitting there to keep hitting enter. 在那种情况下,我不能坐在那里继续打击进入。

Ideally it would be best if I can put some code in the batch file so it can work out where it was started from, and then pause or not as appropriate. 理想情况下,最好是我可以在批处理文件中放入一些代码,以便它可以解决它从哪里开始,然后暂停或不适当。

Use: 使用:

cmd /K myBatch.bat

as your shortcut. 作为你的捷径。

Here is a solution that should work well and take into consideration the possibility that a batch-file might call another batch-file ("Nested"). 这是一个应该运行良好的解决方案,并考虑批处理文件可能调用另一个批处理文件(“嵌套”)的可能性。

You could use Find, to look for "/c" which should not be present if the batch-file is run from a "Command Prompt": 你可以使用Find来查找“/ c”,如果批处理文件是从“命令提示符”运行的话,它应该不存在:

echo %cmdcmdline% | find /i "/c"

But, you could do a more "robust" test by using Find to search for a longer string, or the batch-file name. 但是,您可以通过使用“查找”搜索更长的字符串或批处理文件名来进行更“强大”的测试。

The "Find" command will not work properly if the search string has (") double-quotes within it. To work around that, you can use environment variable substitution to "adjust" the string so it plays nice with Find: 如果搜索字符串中包含(“)双引号,则”查找“命令将无法正常工作。要解决此问题,您可以使用环境变量替换来”调整“字符串,以便它与查找:

set newcmdcmdline=%cmdcmdline:"=-%

This will typically return: 这通常会返回:

if the batch-file is run from a "Command Prompt"  
    newcmdcmdline=-C:\Windows\system32\cmd.exe-

if the batch-file is run by clicking on the the batch-file  
(or the batch-file shortcut) from "Windows Explorer"  
    newcmdcmdline=cmd /c --D:\Path\test.cmd- -

Then you can use "Find" to test like: 然后你可以使用“查找”测试如下:

    echo %newcmdcmdline% | find /i "cmd /c --"
or
    echo %newcmdcmdline% | find /i "cmd /c --%~dpf0%-"

Next, you need to decide if you want a "Nested" batch-file to behave as if you executed it in the same way as the calling batch-file, or if you want nested batch-files to always behave as if they were executed from a "Command Prompt". 接下来,您需要确定是否希望“嵌套”批处理文件的行为就像您以与调用批处理文件相同的方式执行它,或者您希望嵌套批处理文件始终表现得像执行它们一样来自“命令提示符”。

Consider that if you are running in a nested batch-file, then testing for this: 考虑一下,如果您在嵌套的批处理文件中运行,那么测试一下:

echo %newcmdcmdline% | find /i "cmd /c --%~dpf0%-"

will always fail (no match) because %newcmdcmdline% contains the name of the outermost batch-file, not the nested batch-file. 将始终失败(不匹配),因为%newcmdcmdline%包含最外面的批处理文件的名称,而不是嵌套的批处理文件。

So the first solution will behave the same for the calling batch-file, and all nested batch-files. 因此,第一个解决方案对于调用批处理文件和所有嵌套批处理文件的行为都相同。 Also perfect if you don't call any batch-files: 如果您不调用任何批处理文件,也是完美的:

In all batch-files (calling and nested) that you care to make this test, add these lines, typically near the top of the batch-files (you may exclude the echo-statements if you wish): 在您需要进行此测试的所有批处理文件(调用和嵌套)中,添加这些行,通常位于批处理文件的顶部附近(如果需要,可以排除echo语句):

if not defined withincmd call :testshell
if %withincmd% EQU 0 echo This batch-file: %~dpf0 was executed directly (from Windows Explorer, ...).
if %withincmd% EQU 1 echo This batch-file: %~dpf0 was executed from within a Command Prompt
rem if %withincmd% EQU 0 pause

Then, somewhere within each batch-file, add the testshell sub-function: 然后,在每个批处理文件中的某处,添加testshell子函数:

goto :EOF
:testshell
rem "Nested" batch-files won't see this because withincmd is already defined
if not defined newcmdcmdline set newcmdcmdline=%cmdcmdline:"=-%
set withincmd=1
echo %newcmdcmdline% | find /i "cmd /c --%~dpf0%-"
if %errorlevel% EQU 0 set withincmd=0
goto :EOF

You only make the conditional call to "testshell" one time, at the top of the outermost batch-file. 您只能在最外面的批处理文件的顶部对“testshell”进行一次条件调用。

In some situations, you may want to have only the "outermost" batch-file behave differently if it is executed from a "Command Prompt" versus if it is run by clicking on the the batch-file (or the batch-file shortcut) from "Windows Explorer". 在某些情况下,如果从“命令提示符”执行,则可能只希望“最外层”批处理文件的行为不同,而不是通过单击批处理文件(或批处理文件快捷方式)运行它来自“Windows资源管理器”。 So, batch-files called from the "outermost" batch-file will always behave the same regardless of how they are run. 因此,无论它们如何运行,从“最外层”批处理文件调用的批处理文件总是会表现相同。

For this to work, you have a few choices. 为此,您可以选择一些。

1) Save the value of "withincmd" before you call another batch-file, and restore the previous value of "withincmd" after the called batch-file returns. 1)在调用另一个批处理文件之前保存“withincmd”的值,并在调用的批处理文件返回后恢复之前的“withincmd”值。 This is a little involved for most cases. 对于大多数情况,这有点涉及。

2) Use a "globally-unique" variable name for "withincmd" in each batch-file. 2)在每个批处理文件中为“withincmd”使用“全局唯一”变量名称。

3) Execute the "Find" command each time you need to know how the current batch-file was run. 3)每次需要知道当前批处理文件的运行方式时,执行“查找”命令。

4) Increment a variable on entry to a batch-file and decrement it on batch-file exit, then only test how batch-file was run if count-variable=1 4)在进入批处理文件时递增变量并在批处理文件退出时递减它,然后仅测试如果count-variable = 1则运行批处理文件的方式

Method 3 is the easiest, but has the downside that if the outermost batch-file is called from itself (as in recursion) or another batch-file, the test variable (withincmd) will not be properly set. 方法3是最简单的,但其缺点是如果从自​​身(如递归)或另一个批处理文件调用最外面的批处理文件,则不会正确设置测试变量(withincmd)。

Here's how to do it using method 3: 以下是使用方法3的方法:

In all batch-files (calling and nested) that you care to make this test, add these lines, typically near the top of the batch-files (you may exclude the echo-statements if you wish): 在您需要进行此测试的所有批处理文件(调用和嵌套)中,添加这些行,通常位于批处理文件的顶部附近(如果需要,可以排除echo语句):

call :testshell
if %withincmd% EQU 0 echo This batch-file: %~dpf0 was executed directly (from Windows Explorer, ...).
if %withincmd% EQU 1 echo This batch-file: %~dpf0 was executed from (or Nested) within a Command Prompt
rem if %withincmd% EQU 0 pause

Then, somewhere within each batch-file, add the testshell sub-function: 然后,在每个批处理文件中的某处,添加testshell子函数:

goto :EOF
:testshell
if not defined newcmdcmdline set newcmdcmdline=%cmdcmdline:"=-%
set withincmd=1
echo %newcmdcmdline% | find /i "cmd /c --%~dpf0%-"
if %errorlevel% EQU 0 set withincmd=0
goto :EOF

In this case, you have to call "testshell" once, at the top of the EACH batch-file, then again after you have returned from calling another batch-file (or call "testshell" each time you need to know how the current batch-file was run). 在这种情况下,您必须在EACH批处理文件的顶部调用“testshell”一次,然后在调用另一个批处理文件(或每次需要知道当前如何调用“testshell”)后再次调用批处理文件已运行)。

Here's how to do it using method 4: 以下是使用方法4的方法:

In all batch-files (calling and nested) that you care to make this test, add these lines, typically near the top of the batch-files (you may exclude the echo-statements if you wish): 在您需要进行此测试的所有批处理文件(调用和嵌套)中,添加这些行,通常位于批处理文件的顶部附近(如果需要,可以排除echo语句):

if not defined nestinglevel set nestinglevel=0
set /A nestinglevel=nestinglevel+1
call :testshell
if %withincmd% EQU 0 echo This batch-file: %~dpf0 was executed directly (from Windows Explorer, ...).
if %withincmd% EQU 1 echo This batch-file: %~dpf0 was executed from (or Nested) within a Command Prompt
rem if %withincmd% EQU 0 pause

Then, somewhere within each batch-file, add the testshell sub-function: 然后,在每个批处理文件中的某处,添加testshell子函数:

goto :EOF
:testshell
if not defined newcmdcmdline set newcmdcmdline=%cmdcmdline:"=-%
set withincmd=1
if %nestinglevel% GEQ 2 goto :EOF
echo %newcmdcmdline% | find /i "cmd /c --%~dpf0%-"
if %errorlevel% EQU 0 set withincmd=0
goto :EOF

Also, remember to decrement the variable when you exit one batch-file to return to the calling batch-file: 此外,请记住在退出一个批处理文件以返回到调用批处理文件时递减该变量:

set /A nestinglevel=nestinglevel-1

In this case, you have to call "testshell" once, at the top of the EACH batch-file, then again after you have returned from calling another batch-file (or call "testshell" each time you need to know how the current batch-file was run). 在这种情况下,您必须在EACH批处理文件的顶部调用“testshell”一次,然后在调用另一个批处理文件(或每次需要知道当前如何调用“testshell”)后再次调用批处理文件已运行)。

In all cases, test %withincmd% to determine how the current batch-file was run, like this: 在所有情况下,测试%withincmd%以确定当前批处理文件的运行方式,如下所示:

if %withincmd% EQU 0 pause
if %withincmd% EQU 1 goto :EOF

at the end of file print 在文件打印结束时

pause

it will wait for anykey input 它会等待任何键输入

My problem was similar - I wanted a pause at the very end if called from Windows Explorer, but no pause if in a command window. 我的问题很相似 - 如果从Windows资源管理器调用,我想在最后停顿,但如果在命令窗口中没有暂停。 I came up with this. 我想出了这个。 At the top of every batch file, put this: 在每个批处理文件的顶部,放置:

if "%parent%"=="" set parent=%~0
if "%console_mode%"=="" (set console_mode=1& for %%x in (%cmdcmdline%) do if /i "%%~x"=="/c" set console_mode=0)

and then at end 然后结束

if "%parent%"=="%~0" ( if "%console_mode%"=="0" pause )

It handles nested batch calls, where you only want to pause at the end of the original batch file, not in nested batch files. 它处理嵌套批处理调用,您只想在原始批处理文件的末尾暂停,而不是在嵌套批处理文件中。 In a nested batch file, %parent% is already set to original caller so it won't equal the nested %~0 . 在嵌套批处理文件中, %parent%已设置为原始调用方,因此它不等于嵌套的%~0 If you have bat1 which calls bat2 , it leaves open the option of double clicking bat2 in Explorer - in that context bat2 will pause at end, whereas if bat1 calls bat2 , then bat2 won't pause at the end (only bat1 will pause). 如果你有bat1调用bat2 ,它会在资源管理器中打开双击bat2的选项 - 在那种情况下bat2会在结束时暂停,而如果bat1调用bat2 ,那么bat2将不会在结尾处暂停(只有bat1会暂停) 。

The & statement separator helps avoid visual clutter for something which is secondary to the main function. &语句分隔符有助于避免对主函数次要的东西造成视觉混乱。 If you don't like the look of it, just put each statement on a new line. 如果您不喜欢它的外观,只需将每个语句放在一个新行上即可。

This approach looks for /C as one of the params in the launch command ( %cmdcmdline% ). 此方法将/C视为启动命令中的一个参数( %cmdcmdline% )。 It assumes your batch files don't use the /C option. 它假定您的批处理文件不使用/C选项。 If you use /C yourself, then you need to instead check if %COMSPEC% appears within %cmdcmdline% (use FINDSTR ). 如果您自己使用/C ,则需要检查%COMSPEC%出现在%cmdcmdline% (使用FINDSTR )。 When Explorer launches a bat file, its %cmdcmdline% includes %COMSPEC% eg C:\\Windows\\System32\\cmd.exe /C double_clicked_batch_file_name . 当Explorer启动bat文件时,其%cmdcmdline%包括%COMSPEC%例如C:\\Windows\\System32\\cmd.exe /C double_clicked_batch_file_name In a command window, %cmdcmdline% just has cmd.exe in it (no path). 在命令窗口中, %cmdcmdline%只包含cmd.exe (无路径)。 I use CALL mybat rather than cmd.exe /C mybat , but you may have to work with existing batch files. 我使用CALL mybat而不是cmd.exe /C mybat ,但您可能必须使用现有的批处理文件。

you can just add params to the batch call and handle conditionally pause statement in your batch. 您只需将params添加到批处理调用中,并在批处理中处理有条件的暂停语句。 So when started from command line or dblclick the batch can pause, when called from others batches with a /nopause param don't pause. 因此,当从命令行或dblclick启动时,批处理可以暂停,当从其他批处理调用时,/ nopause param不会暂停。

use "pause" in the batch file at the end, and it will wait for the user input 最后在批处理文件中使用“pause”,它将等待用户输入

HTH HTH

Would the pause command work? 暂停命令会起作用吗?

Microsoft Documentation on pause 关于暂停的Microsoft文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 双击时暂停批处理文件,但从控制台窗口运行时不暂停? - Pausing a batch file when double-clicked but not when run from a console window? 批处理文件在命令提示符下运行正常,但双击时给出错误 - Batch file runs fine in command prompt but gives error when double-clicked 在 Window OS 中双击时如何在空闲 shell 中运行脚本? - How to run a script in Idle shell when double-clicked in Window OS? 执行双击的Java jar文件时的输出流 - Output stream while executing a double-clicked java jar file 双击时批处理文件闪烁并关闭 - Batch file flashes and closes when double Clicked 是否可以构建一个双击时不显示控制台窗口的控制台应用程序? - Is it possible to build a Console app that does not display a console Window when double-clicked? 计划任务调用的批处理文件在计划时抛出错误,双击运行良好 - Batch file called by scheduled task throws error when scheduled, runs fine when double clicked Windows Batch,如何暂停然后关闭窗口? - Windows Batch, how to pause then close window? Windows批处理CMD窗口即使在文件末尾暂停也可以快速关闭 - Windows Batch CMD windows Closes fast even with Pause at end of File 如何使用将暂停执行直到域启动的批处理文件在 Windows 中启动 Glassfish 服务? - How to start Glassfish service in Windows using batch file that will pause execution untill domain is started?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM