简体   繁体   English

在安装软件后,寻找从批处理文件中设置Windows环境变量的更好方法

[英]Looking for a better way to set up Windows environment variables from batch file after installing a software

I am building a Windows .bat script to automatically compile and install the Geant4 toolkit from CERN (but my following questions are independent of which of software I want to deal with). 我正在构建一个Windows .bat脚本来自动编译和安装来自CERN的Geant4工具包(但我的以下问题与我想要处理的软件无关)。 What I managed to do so far seems to work OK-ish, but I am not satisfied with how the environment variables are set at the end of the script. 到目前为止我设法做的似乎工作正常,但我不满意在脚本结束时如何设置环境变量。

To complete the installation, I have to set up environment variables to indicate the path to required data-sets, and C++ include and library directories (I choose to modify the "PATH" variable for these lasts). 要完成安装,我必须设置环境变量以指示所需数据集的路径,以及C ++包含和库目录(我选择修改这些持续时间的“PATH”变量)。 I want to set them up for the current script (using set command) and for the next executions (using setx command) 我想为当前脚本(使用set命令)和下一次执行(使用setx命令)设置它们

The script I am using now to do that is the following: 我现在使用的脚本如下:

REM to get the path to directory where this bat file is executed from.
set G4_bat_file_dir=%~dp0

REM set the environement variables for next cmd runs

REM adding to local (temporary) PATH
set G4dataset_RootDir="%G4_bat_file_dir%\install\share\Geant4-10.4.3\data\"

REM adding environment variables for current and next cmd executions
setx G4dataset_RootDir "%G4_bat_file_dir%\install\share\Geant4-10.4.3\data\"

setx G4ABLADATA %G4dataset_RootDir%\G4ABLA3.1
setx G4ENSDFSTATEDATA %G4dataset_RootDir%\G4ENSDFSTATE2.2
setx G4LEDATA %G4dataset_RootDir%\G4EMLOW7.3
setx G4LEVELGAMMADATA %G4dataset_RootDir%\PhotonEvaporation5.2
setx G4NEUTRONHPDATA %G4dataset_RootDir%\G4NDL4.5
setx G4NEUTRONXSDATA %G4dataset_RootDir%\G4NEUTRONXS1.4
setx G4PIIDATA %G4dataset_RootDir%\G4PII1.3
setx G4RADIOACTIVEDATA %G4dataset_RootDir%\RadioactiveDecay5.2
setx G4REALSURFACEDATA %G4dataset_RootDir%\RealSurface2.1.1
setx G4SAIDXSDATA %G4dataset_RootDir%\G4SAIDDATA1.1

set G4ABLADATA=%G4dataset_RootDir%\G4ABLA3.1
set G4ENSDFSTATEDATA=%G4dataset_RootDir%\G4ENSDFSTATE2.2
set G4LEDATA=%G4dataset_RootDir%\G4EMLOW7.3
set G4LEVELGAMMADATA=%G4dataset_RootDir%\PhotonEvaporation5.2
set G4NEUTRONHPDATA=%G4dataset_RootDir%\G4NDL4.5
set G4NEUTRONXSDATA=%G4dataset_RootDir%\G4NEUTRONXS1.4
set G4PIIDATA=%G4dataset_RootDir%\G4PII1.3
set G4RADIOACTIVEDATA=%G4dataset_RootDir%\RadioactiveDecay5.2
set G4REALSURFACEDATA=%G4dataset_RootDir%\RealSurface2.1.1
set G4SAIDXSDATA=%G4dataset_RootDir%\G4SAIDDATA1.1

setx Geant4_DIR %G4_bat_file_dir%\install\lib\Geant4-10.4.3\

REM adding to PATH the paths to libraries and includes for Qt4 and Geant4.

setx PATH "%G4_bat_file_dir%\install\lib;%G4_bat_file_dir%\install\bin;%G4_bat_file_dir%\xerces-c\instal\bin;%G4_bat_file_dir%\xerces-c\instal\lib;%G4_bat_file_dir%Qt4\install\bin;%G4_bat_file_dir%Qt4\install\lib;%PATH%"

The paths %G4_bat_file_dir%\\install\\lib;%G4_bat_file_dir%\\install\\bin;%G4_bat_file_dir%\\xerces-c\\instal\\bin;%G4_bat_file_dir%\\xerces-c\\instal\\lib;%G4_bat_file_dir%Qt4\\install\\bin;%G4_bat_file_dir%Qt4\\install\\lib being the ones I want to append. 路径%G4_bat_file_dir%\\install\\lib;%G4_bat_file_dir%\\install\\bin;%G4_bat_file_dir%\\xerces-c\\instal\\bin;%G4_bat_file_dir%\\xerces-c\\instal\\lib;%G4_bat_file_dir%Qt4\\install\\bin;%G4_bat_file_dir%Qt4\\install\\lib是我要追加的那些。

This is a screenshot of the environment variables set-up I get after running the script 2 times : 这是我运行脚本2次后获得的环境变量设置的屏幕截图:

http://djienne.free.fr/env.png http://djienne.free.fr/env.png

This is far from ideal, there several things that I am not happy with: 这远非理想,有几件我不满意的事情:

  • all the paths in the variables get fully expended, and then also the PATH variable gets too long and I get the error "WARNING: The data being saved is truncated to 1024 characters." 变量中的所有路径都被完全耗尽,然后PATH变量也变得太长,我得到错误“警告:保存的数据被截断为1024个字符。”

  • If I run the script twice in a row, it produces duplicates in the PATH entries (and everything above the 1024 characters limit is truncated) 如果我连续两次运行该脚本,它会在PATH条目中生成重复项(超过1024个字符限制的所有内容都会被截断)

  • also, if I put this code at the end of my main compilation/installation script it gives the error 'setx' is not recognized as an internal or external command, operable program or batch file. 另外,如果我将此代码放在主编译/安装脚本的末尾,则会将错误'setx' is not recognized as an internal or external command, operable program or batch file. and so the environment variables are not created/modified. 因此不会创建/修改环境变量。 But if I run this script as a separate .bat file, it works. 但是,如果我将此脚本作为单独的.bat文件运行,它就可以运行。 So there is something I don't understand. 所以有一些我不明白的东西。 (I specify that I always do "run as administrator" to run the scripts.) (我指定我总是“以管理员身份运行”来运行脚本。)

Thanks in advance for the help. 先谢谢您的帮助。

Following the advice from the comments, I built a batch script launch_visual_studio.bat at the top level of my project, to launch Visual Studio with an updated local PATH. 根据评论的建议,我在项目的顶层构建了一个批处理脚本launch_visual_studio.bat ,以使用更新的本地PATH启动Visual Studio。 The file contains the code: 该文件包含以下代码:

@echo off

REM Set the environment

set G4_bat_file_dir=%~dp0

set QTDIR=%G4_bat_file_dir%Qt5\qt-5.6.3\

set QMAKESPEC=win32-msvc2015

set Geant4_DIR=%G4_bat_file_dir%install\lib\Geant4-10.4.3\

REM split into two parts for readability
set PATH=%PATH%;%G4_bat_file_dir%install\bin;%G4_bat_file_dir%install\lib;%G4_bat_file_dir%install\include\Geant4
set PATH=%PATH%;%QTDIR%lib;%QTDIR%bin;%QTDIR%include

REM launch visual studio

"%vs140comntools%..\IDE\devenv.exe"

This works for visual studio 2015, but will be different for other versions. 这适用于Visual Studio 2015,但对于其他版本会有所不同。

For the environment variables other than PATH , QTDIR and Geant4_DIR , since they have very specific names ( G4ABLADATA , G4ENSDFSTATEDATA , ...), it seems fine to set them permanently using setx , as shown previously. 对于除PATHQTDIRGeant4_DIR之外的环境变量,由于它们具有非常特定的名称( G4ABLADATAG4ENSDFSTATEDATA ,...),使用setx永久设置它们似乎很好,如前所示。

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

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