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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/886848/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 12:31:43  来源:igfitidea点击:

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

windowsbatch-file

提问by Scott Langham

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. I'd like a solution where I can use the batch file without having to do anything too special?

当我从命令行调用批处理文件时,我不想传递 /nopause 参数或其他东西。我想要一个解决方案,我可以使用批处理文件而不必做任何特别的事情?

Thanks.

谢谢。

NOTEI 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.

理想情况下,最好是我可以在批处理文件中放入一些代码,这样它就可以确定它是从哪里开始的,然后暂停或不适当。

回答by Matthew Flaschen

Use:

用:

cmd /K myBatch.bat

as your shortcut.

作为你的捷径。

回答by Kevin Fegan

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.

但是,您可以通过使用 Find 搜索更长的字符串或批处理文件名来进行更“稳健”的测试。

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:

如果搜索字符串中有 (") 双引号,则“Find”命令将无法正常工作。要解决此问题,您可以使用环境变量替换来“调整”字符串,使其与 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". So, batch-files called from the "outermost" batch-file will always behave the same regardless of how they are run.

在某些情况下,如果从“命令提示符”执行它与通过单击批处理文件(或批处理文件快捷方式)运行相比,您可能只想让“最外层”批处理文件的行为有所不同来自“Windows 资源管理器”。因此,从“最外层”批处理文件调用的批处理文件将始终表现相同,无论它们如何运行。

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. This is a little involved for most cases.

1) 在调用另一个批处理文件之前保存“withincmd”的值,并在调用的批处理文件返回后恢复“withincmd”的先前值。对于大多数情况,这有点涉及。

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 是最简单的,但有一个缺点,如果最外层的批处理文件是从自身(如递归)或另一个批处理文件调用的,则测试变量(incmd)将无法正确设置。

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).

在这种情况下,您必须在每个批处理文件的顶部调用一次“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).

在这种情况下,您必须在每个批处理文件的顶部调用一次“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

回答by John D

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. I came up with this. At the top of every batch file, put this:

我的问题是类似的 - 如果从 Windows 资源管理器调用,我想在最后暂停,但如果在命令窗口中没有暂停。我想出了这个。在每个批处理文件的顶部,放置:

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. If you have bat1which calls bat2, it leaves open the option of double clicking bat2 in Explorer - in that context bat2will pause at end, whereas if bat1calls bat2, then bat2won't pause at the end (only bat1will pause).

它处理嵌套的批处理调用,您只想在原始批处理文件的末尾暂停,而不是在嵌套的批处理文件中暂停。在嵌套的批处理文件中,%parent%已设置为原始调用者,因此它不等于嵌套的%~0. 如果您有bat1which calls bat2,它会打开在资源管理器中双击 bat2 的选项 - 在该上下文bat2中将在最后暂停,而如果bat1call 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 /Cas one of the params in the launch command (%cmdcmdline%). It assumes your batch files don't use the /Coption. If you use /Cyourself, then you need to instead check if %COMSPEC%appears within %cmdcmdline%(use FINDSTR). When Explorer launches a bat file, its %cmdcmdline%includes %COMSPEC%eg C:\Windows\System32\cmd.exe /C double_clicked_batch_file_name. In a command window, %cmdcmdline%just has cmd.exein it (no path). I use CALL mybatrather than cmd.exe /C mybat, but you may have to work with existing batch files.

这种方法/C在启动命令 ( %cmdcmdline%) 中寻找参数之一。它假定您的批处理文件不使用该/C选项。如果你/C自己使用,那么你需要检查是否%COMSPEC%出现在%cmdcmdline%(use FINDSTR) 中。当资源管理器启动一个 bat 文件时,它%cmdcmdline%包括%COMSPEC%例如C:\Windows\System32\cmd.exe /C double_clicked_batch_file_name. 在命令窗口中,%cmdcmdline%只有cmd.exe它(没有路径)。我使用CALL mybat而不是cmd.exe /C mybat,但您可能必须使用现有的批处理文件。

回答by Sergei Kovalenko

at the end of file print

在文件末尾打印

pause

it will wait for anykey input

它将等待任意键输入

回答by Andrea Balducci

you can just add params to the batch call and handle conditionally pause statement in your batch. 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 参数的其他批处理中调用时,不要暂停。

回答by Anand Shah

use "pause" in the batch file at the end, and it will wait for the user input

最后在批处理文件中使用“暂停”,它将等待用户输入

HTH

HTH

回答by Rihan Meij

Would the pause command work?

暂停命令会起作用吗?

Microsoft Documentation on pause

暂停的 Microsoft 文档