批处理文件命令 PAUSE 不起作用

发布于 2024-10-11 13:28:20 字数 209 浏览 4 评论 0原文

我正在创建一个简单的批处理文件来协助完成一些事情,并且我还打印了一些说明,希望用户在退出之前看到这些说明。目前,窗口关闭得很快。所以我在文件末尾添加了 PAUSE ,但它不起作用。

我查看了有关 SO 的其他问题,并检查以确保行结尾是 CRLF 并且我在文件末尾有 CRLF

有什么建议吗?

I am creating a simple batch file to assist in a few things, and I have some instructions that it prints out as well that I want the user to see before exit. Currently, the window closes very quickly. So I added PAUSE at the end of the file, but it does not want to work.

I looked at other questions on SO and have checked to make sure the line endings are CRLF and that I have CRLF at the end of the file.

Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

简单 2024-10-18 13:28:20

如果最后一个命令失败,暂停将不起作用。

您可以通过将“call”放在您正在运行的命令前面(无论暂停之前是什么命令)来修复它,然后暂停就会起作用。

例如,我有一个 phpunit 批处理文件,如下所示:

phpunit tests/sometests.php
pause

当 phpunit 失败时,它只是退出而不暂停。将其更改为这样可以正确暂停:

call phpunit tests/sometests.php
pause

If the last command fails pause won't work.

You can fix it by putting "call" in front of the command you are running (whatever command is before the pause) then the pause will work.

So for example I had a phpunit batch file that looked like this:

phpunit tests/sometests.php
pause

When phpunit failed it just exited without pausing. Changing it to this made it pause correctly:

call phpunit tests/sometests.php
pause
深海不蓝 2024-10-18 13:28:20

暂停前的最后一条命令是否执行成功?介意分享你的脚本 - 至少最后几个命令吗?

或者,由于您似乎使用的是 Windows7,请尝试 Timeout 命令并查看是否有效。

Does the last command before pause execute successfully? Mind sharing your script - at least last few commands?

Alternatively, since you seem to be using Windows7, try Timeout command and see if that is working.

为人所爱 2024-10-18 13:28:20

我什至在回声上也遇到了问题......
假设它是由长批处理文件引起的...暂停正在执行但它没有暂停,几乎就像执行暂停后按下了一个键。

尝试了上面建议的解决方案;没有一个起作用。

因此,仅供将来参考,这就是我所做的:

基本上只是“暂停> nul &&暂停> nul”;每次都有效。

@echo off

CALL :ForcePause "Press any key to resume."
ECHO.
ECHO Hello World!
ECHO.
CALL :ForcePause "Press any key to exit."

EXIT

REM You can remove echo if you don't want to pass custom string for pause
:ForcePause
echo %~1
pause > nul && pause > nul
GOTO :EOF

I was having issues even on echo...
assuming it was caused by long batch file... Pause was executing but it was not pausing, it was almost like that it was pressing a key after Pause was executed.

Tried suggested solutions above; none worked.

So just for future reference, here is what I did:

Basically just "pause > nul && pause > nul"; works every time.

@echo off

CALL :ForcePause "Press any key to resume."
ECHO.
ECHO Hello World!
ECHO.
CALL :ForcePause "Press any key to exit."

EXIT

REM You can remove echo if you don't want to pass custom string for pause
:ForcePause
echo %~1
pause > nul && pause > nul
GOTO :EOF
十年九夏 2024-10-18 13:28:20

我想我知道问题出在哪里了,我也遇到了同样的问题。因此,如果您这样做,您将创建一个新文件,并将所有批次信息放入其中,然后像普通文本一样保存,之后只需重命名扩展名,它就不起作用了:)。您必须通过文本编辑器保存它,然后从那里选择“批处理文件(.bat;.cmd;*.nt)”,例如 Notepad++,这可能是因为编码的原因,所以就像这样,我想会没事的。 GL! :)

I think i know where is the issue, i had the same problem. So if you are making it like this, you creating a new file and put all the batch info inside it and save it like normal text and after this just rename the extension it won't work :). You have to save it via the text editor and from there you have to choose "Batch file(.bat;.cmd;*.nt)" for example Notepad++, its probably because of the encoding so do it like this and i thnk it will be ok. GL ! :)

只有一腔孤勇 2024-10-18 13:28:20

除了蒂姆的回答之外,如果您希望窗口始终显示,您可以编写 .bat 文件,如下所示:

call phpunit tests/sometests.php
cmd /k

Just addition to Tim's answer, if you want the window to always display, you can write the .bat file like:

call phpunit tests/sometests.php
cmd /k
[旋木] 2024-10-18 13:28:20

我有同样的问题。出现这种情况有两个原因:
第一个是以管理员身份运行批处理文件

我已将批处理文件移至桌面并以管理员身份尝试,效果很好。
我已尝试将批处理文件移动到任何磁盘根目录(如 C:\ D:\ 等)并且它可以正常工作。
我尝试将批处理文件移动到任何带有空格的目录,并且它可以正常工作。

第二个也是主要原因是:批处理文件中存在一些特殊字符(例如 @ 或 & 或类似的字符)目录

只需找到这个特殊字符并将其从文件夹名称中删除即可。并且它会正常工作。

I have the same problem. This occurs because of two reasons:
The first one is when running the batch file as administrator.

I have moved my batch file to desktop and try it as administrator and it works fine.
I have tried moving the batch file to any disk root (like C:\ D:\ etc.) and it works correctly.
I have tried moving the batch file to any directory with spaces and it works correctly.

The second and main reason is: There was some special characters (like @ or & or something like that) in the batch file's directory.

Just find this special character and remove it from the folder name. And it will work correctly.

为人所爱 2024-10-18 13:28:20

我无法理解为什么 pause 在我的脚本中只是短暂暂停。这里的答案帮助我解决了问题,我终于弄清楚了我的情况发生了什么:

start /b

如果您使用 start /b 运行应用程序,应用程序可能会将一些输出发送到延迟后的标准输出。如果在运行暂停命令时出现该输出,它将被解释为击键,并且脚本自然会继续。如果您将 start /b 与连接到远程计算机的命令一起使用,那么您肯定会遇到此问题:

start /b plink [email protected] -pw raspberry "sudo apt-get update"
pause

解决方法是使用 pause >nul &&改为暂停

I couldn't understand why pause was only briefly pausing in my script. The answers here helped me solve the problem, and I finally figured out what was happening in my case:

start /b

If you run an application with start /b it is possible the application will send some output to stdout after a delay. If that output comes when the pause command is running, it will be interpreted as keystrokes and, naturally, the script will continue. You will most certainly have this issue if you use start /b with commands that connect to remote computers:

start /b plink [email protected] -pw raspberry "sudo apt-get update"
pause

The workaround is to use pause >nul && pause instead

心碎的声音 2024-10-18 13:28:20

仅供参考:上面显示的内容在 Win7 中对我不起作用。
然而,这最终起作用了:

PAUSE 0<CON

这也不适用于“超时”。
如果没有它,它会得到这样的结果:

ERROR: input redirection not supported,...

有了它,它会得到这样的结果:

ERROR: The handle is invalid.

在我的例子中,执行此操作后会发生这种情况:

start /WAIT cmd.exe /V:ON /k "set IEdir & pushd %IEdir% & cd & call "C:\Program Files\Internet Explorer\iexplore.exe" & set XsetRC1=!errorlevel! & set Xset & echo Xrc1=!XsetRC1! & popd & timeout /T 3 & EXIT !XsetRC1!"

其中 IE 返回 RC=1(KILL 也会得到什么)。

然而,对 FireFox 和 Thunderbird 执行完全相同的操作:

start /WAIT cmd.exe /V:ON /k "set FFdir & pushd %FFdir% & cd & call "C:\FireFox\FireFox.exe" & set XsetRC1=!errorlevel! & set Xset & echo Xrc1=!XsetRC1! & popd & timeout /T 3 & EXIT !XsetRC1!"

其中 FF 和 TB 返回 RC=0,“超时”确实可以正常工作。

另请注意,PAUSE 在 Win10 中不会失败。

FYI: nothing shown above worked for me in Win7.
However, this DID finally work:

PAUSE 0<CON

This also did NOT work for 'timeout'.
Without it, it gets this:

ERROR: input redirection not supported,...

With it, it gets this:

ERROR: The handle is invalid.

In my case, this occurred after doing this:

start /WAIT cmd.exe /V:ON /k "set IEdir & pushd %IEdir% & cd & call "C:\Program Files\Internet Explorer\iexplore.exe" & set XsetRC1=!errorlevel! & set Xset & echo Xrc1=!XsetRC1! & popd & timeout /T 3 & EXIT !XsetRC1!"

where IE returns an RC=1 (what a KILL also gets).

However, doing the exact same for FireFox and Thunderbird:

start /WAIT cmd.exe /V:ON /k "set FFdir & pushd %FFdir% & cd & call "C:\FireFox\FireFox.exe" & set XsetRC1=!errorlevel! & set Xset & echo Xrc1=!XsetRC1! & popd & timeout /T 3 & EXIT !XsetRC1!"

where FF and TB returns an RC=0, 'timeout' DOES work properly.

Also note that PAUSE does NOT fail in Win10.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文