vba 执行批处理文件后停止关闭命令提示符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19739207/
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
Stop command prompt from getting closed after executing a batch file
提问by LoserGuy
I used Shell command from Excel VBA to run a certain .bat file. The CMD appears but exits automatically after all lines were executed. I've added the PAUSE command a the end of the bat. file but it's not working. How to stop command prompt from getting automatically closed?
我使用 Excel VBA 中的 Shell 命令来运行某个 .bat 文件。CMD 出现但在所有行执行后自动退出。我在蝙蝠的末尾添加了 PAUSE 命令。文件,但它不起作用。如何阻止命令提示符自动关闭?
回答by wedwo
Try using the /K switch to prevent exiting after running the batch file like this:
尝试使用 /K 开关来防止在运行批处理文件后退出,如下所示:
cmd /K "path to batch file including file name and extension"
cmd /K "批处理文件的路径,包括文件名和扩展名"
e.g.
例如
cmd /K "C:\FOLDER\BATCH FILE.BAT"
or
或者
cmd /K "C:\FOLDER\BATCH FILE.CMD"
Obviously that won't reveal any faults with your batch code, but it won't exit after running - much the same as running the batch file from the command line.
显然,这不会揭示您的批处理代码的任何错误,但它在运行后不会退出 - 与从命令行运行批处理文件非常相似。
Definitely add some ECHO output lines to indicate progress as it runs and troubleshoot. Perhaps post the file here for more help.
一定要添加一些 ECHO 输出行来指示运行和故障排除的进度。也许在这里发布文件以获得更多帮助。
回答by JuniorIncanter
While perhaps not actually answering the posted question, I stumbled upon this question in a search to a very related-question. I was also trying to stop a command prompt from closing as it was erroring out and closing immediately. For me, it was sufficient for me to view the output as what I really wanted was to be able to see the error in order to debug it.
虽然可能实际上并没有回答发布的问题,但我在搜索一个非常相关的问题时偶然发现了这个问题。我还试图阻止命令提示符关闭,因为它出错并立即关闭。对我来说,查看输出就足够了,因为我真正想要的是能够看到错误以便对其进行调试。
The solution I found was to pipe the output of the command prompt into a text file, like so:
我找到的解决方案是将命令提示符的输出通过管道传输到文本文件中,如下所示:
MyScript.bat >> text.txt
This allowed me to see the error I was getting.
这让我看到了我得到的错误。
回答by Govardhan
Try opening the command prompt and running the batch file. It is possible that the the dos shell is encountering an error and closing. If the error comes before the execution reaches Pause command, it will not pause for you to read the error and it immediately closes off.
尝试打开命令提示符并运行批处理文件。dos shell 可能遇到错误并关闭。如果错误在执行到达暂停命令之前出现,它不会暂停让您阅读错误并立即关闭。
If that doesn't work out, check if you have any exit commands in any of the branches. Alternatively, place echo statements at different places and check if the execution control reaches these echos. This way you can find out if your script if ending at some other branch.
如果这不起作用,请检查在任何分支中是否有任何退出命令。或者,将 echo 语句放在不同的地方,并检查执行控制是否到达这些 echo。通过这种方式,您可以确定您的脚本是否以其他分支结束。