本文介绍了Windows批处理文件不等待命令完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个批处理文件,它立即存在(以管理员身份运行),并不执行其中的命令,但如果我在命令行中指定它,它运行良好并执行所有命令。



这里有什么内容:

 启动/等待msiexec / x SetupServices.msi / qn / l *SetupServices.install.log

启动/等待msiexec / i SetupServices.msi / qn / code>


解决方案

(更正的答案)



首先,如果您在批处理中启动.exe文件,它会更安全,以call作为前缀。



使用start是另一种可能性,但对于这个简单的用例程序不是必须的。



您写的命令不是执行的。所以,显然,你有另一个问题,而不是不等待...完成的问题。
看看你提供的新例子,就是这样。在管理模式下,您必须提供完整路径。有了我的小技巧(%〜dp0,包括已经反斜杠),你仍然可以使用当前目录在批处理文件。



大多数情况下,有管理员权限,这是当前目录路径的问题。有管理员权限的批处理文件不像我们以前一样使用它,它不是从它自己的目录(但在System32主要是)。不依赖CD是编写防弹批处理文件的重要问题。



这是一个很好的例子,结合了这里的其他答案,并解决了一些可能的问题你的情况是:

 调用msiexec / i%〜dp0MySetup.msi/ qb / L * v%〜dp0MySetup。 log
echo Returncode:%ERRORLEVEL%
pause

目录,并假定一个包含日志文件的安装命令行(只有在当前目录中具有写入权限时才有效,如果没有指定具有写访问权限的日志文件的路径,例如%TEMP%\MySetup.log。

注意:请记住使用管理员权限真正启动批处理文件(鼠标右键或在之前打开管理命令shell)


I have a batch file, which exists as soon as start it (run as admin) and does not execute commands that are in it, but if I specify it at the command-line, it runs fine and executes all commands.

Here's what's in it:

start /wait msiexec /x SetupServices.msi /qn /l* "SetupServices.uninstall.log"

start /wait msiexec /i SetupServices.msi /qn /l* "SetupServices.install.log"
解决方案

(Corrected answer)

First, if you start .exe files in a batch, it is safer, to prefix it with "call".Sometimes this is necessary to assure, that the batch is waiting to complete.

Using "start" is another possibility, but for this simple usecase not necessary.

You write that the commands are not executed. So, obviously, you have another problem, instead of the "does not wait.. to complete" problem.Taking a look of your newly provided example, this is the case. In admin mode, you have to provide a full path. With my small trick below ("%~dp0", including already backslash), you can still use the current directory in batchfiles.

Most times, if such a problem occurs with admin rights, this is a problem of the "current directory" path. A batch file with admin rights is not using it in the same way as we were used to, it does not start in it's own directory (but in System32 mostly). Not relying on the CD is an important matter of writing bullet-proof batch files.

A good example batch , combining other answers here, and solving a number of possible problems in your case is:

call msiexec /i "%~dp0MySetup.msi" /qb /L*v "%~dp0MySetup.log"
echo Returncode: %ERRORLEVEL%
pause

It uses the current directory correctly, and assumes an install commandline including a logfile (works only, if you have write access in the current directory, if not specify a path for the logfile with write access like "%TEMP%\MySetup.log".

Attention: Remember to really start the batch file with admin rights (right mouse menu or opening an admin command shell before:)

这篇关于Windows批处理文件不等待命令完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-17 03:46
查看更多