简体   繁体   English

执行批处理脚本时的不同行为

[英]Different behaviours when executing a batch script

I created a batch script that installs some .vsix extensions and launches visual studio. 我创建了一个批处理脚本,该脚本安装了一些.vsix扩展名并启动了Visual Studio。

call "C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\vcvarsall.bat"
start /wait VSIXInstaller.exe /q ABC.vsix
start "" "C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\devenv.exe"

When I execute the script by double clicking, it works perfectly. 当我双击执行脚本时,它可以完美运行。

The next thing I wanted to do is to launch the script at windows startup. 我想做的下一件事是在Windows启动时启动脚本。

I added an entry in the registry, here : 我在注册表中添加了一个条目,在这里:

HKEY_LOCAL_MACHINE -> SOFTWARE -> Microsoft -> Windows - CurrentVersion -> Run

I restared my machine and when windows started up, the script was executed but Visual Studio was launched before the installation of the vsix (extensions). 我重新注视了机器,并在Windows启动时执行了脚本,但在安装vsix(扩展名)之前启动了Visual Studio。

Why I have different behaviours when running the script directly and when it is started by windows ? 为什么在直接运行脚本和由Windows启动脚本时会有不同的行为?

Is there something I have to do to correct that ? 有什么我需要做的更正吗?

VSIXInstaller.exe is started without full path. VSIXInstaller.exe启动时没有完整路径。 Therefore Windows searches for such an application first in current working directory and next in all directories as defined in environment variable PATH . 因此,Windows首先在当前工作目录中搜索该应用程序,然后在环境变量PATH中定义的所有目录中搜索该应用程序。

I suppose that list of directories in PATH differ. 我认为PATH中的目录列表有所不同。

Or the working directory, also called Start in directory, is different as it is not possible to define a working directory for applications or batch files started via Windows registry entry Run . 或工作目录(也称为“ Start in目录中Start in )有所不同,因为无法为通过Windows注册表项Run启动的应用程序或批处理文件定义工作目录。

The solution is to specify VSIXInstaller.exe with complete path in double quotes if needed which then requires also a title in double quotes for command start . 解决方案是在需要时用双引号指定完整路径的VSIXInstaller.exe ,然后还需要在命令双引号中加上标题以start命令。

call "%ProgramFiles(x86)%\Microsoft Visual Studio 11.0\VC\vcvarsall.bat"
start "Install VSIX Extensions" /wait "Path to\VSIXInstaller\VSIXInstaller.exe" /q ABC.vsix
start "Configure IDE Environment" "%ProgramFiles(x86)%\Microsoft Visual Studio 11.0\Common7\IDE\devenv.exe"

Hint: To find out what happens on execution of this batch file on Windows startup, specify in Windows registry a different batch file which contains the commands: 提示:要了解在Windows启动时执行此批处理文件会发生什么,请在Windows注册表中指定另一个包含命令的批处理文件:

echo Working directory is %CD% 1>"C:\Temp\BatchStdout.txt"
call "Full\Path\To\Above\BatchFile.bat" 1>>"C:\Temp\BatchStdout.txt" 2>"C:\Temp\BatchStderr.txt"

Restart Windows and look on the files BatchStdout.txt and BatchStderr.txt created in directory C:\\Temp which of course must already exist before Windows restart. 重新启动Windows,然后查看在目录C:\\Temp创建的文件BatchStdout.txtBatchStderr.txt ,它们当然必须在Windows重新启动之前已经存在。

By the way: If this batch file should not run for all user accounts, it would be better to use 顺便说一句:如果不应为所有用户帐户运行此批处理文件,则最好使用

HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Run

Or you put a shortcut to the batch file into the folder Startup of the Windows start menu (all users profile or your user profile). 或者,您将批处理文件的快捷方式放入Windows开始菜单(所有用户配置文件或您的用户配置文件)的文件夹Startup中。

I would prefer the Startup folder instead of a Windows registry Run entry for such a task. 对于这种任务,我希望使用Startup文件夹而不是Windows注册表Run条目。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM