I am using a .bat
file to automate some tasks for my engine (once it is freshly cloned from the repository). O开发者_如何学Gone of the tasks is setting the environment variables. I am using the SETX
command and set the path of the named variable to be %CD%
, i.e. the directory the installer was run from.
This works well, although the user needs to log-off/log-on
which may be annoying. This log-off/log-on
cycle is not required if the variable is set as the system variable (I am not sure why but restarting Visual Studio has no effect on user environment variables... that is, it detects no changes but it will detect changes in system variables). So I proceeded to use the -m
command. Unfortunately, that requires that the batch file has admin privileges. Not a problem; I ran the batch file as administrator. Well, now I ran into a problem. The current directory variable, %CD%
changed from the directory the installer was run from, to C:\Windows\System32
.
So now onto the question. How do I set system environment variables via a batch file which utilizes %CD%
without it defaulting to C:\Windows\System32
. Just as a note, quite a few people use the installer and I would like this process to be as painless and mistakes free as possible. Which means, no manual entry is preferred. Currently, if run without admin privileges and without -m
, the only thing needed is a log-off/log-on
cycle. Otherwise, everything is automated.
%0
is the name of your batch file. You can use %~dp0 with pushd
to change to the directory where your batch file is located and work your way to any directory from there. So the general structure of such a batch file is :
@echo off
pushd %~dp0
rem batch file commands go here
popd
As for your Visual Studio issue... User mode environment variables are available for each process started after the environment variable has been set. But for some reason, the current process doesn't receive it. But the Explorer.exe (who handles that start menu and the run command seems to fetch a new copy of the environment each time it needs it.
A process started that way will have the new environment, whereas a process launched from the command line will inherit the old environment, without the new variables set by setx
.
You can mitigate this problem by changing your batch file to both set
and setx
the variables.
Here is some code to get you started. It will
- Show the current working directory
- Change the the directory where the script resides
- Set the variable MYTEST in the shell it's running
- Set the variable in the user's environment
- Launch Notepad from the shell, so you can try and open a file name %MYTEST%
- Try it with the %, it works
- Notice how the current directory was changed, btw.
- Ask you to start notepad manually from the start menu
Here is the code, HTH
@echo off
::This is where we start
echo Current directory is %CD%
echo %0
::We change the current directory to where the script is running
pushd %~dp0
echo Current directory is %CD%
::if you want, you can move relatively from it
cd..
::Set an environement variable
set MYTEST=%~f0
::Make a copy avaiable to other processes
setx MYTEST "%MYTEST%"
::Now I should be able to fire notepad and open %%MYTEST%%
::(you can use the %% sign in the open box)
::Let's start a copy from this process
::
echo Starting notepad, open the file %%MYTEST%%, you should this
echo file thanks to the set statement.
notepad
echo Now launch Notepad from the start menu and open the file %%MYTEST%%,
echo you should this file thanks to the setx statement.
::Wherever you end up, restore the current directory
popd
echo Current directory is %CD%
consider, you want to setup ant using bat script do the following:
echo off
echo SETTING UP ANT FOR THE BUILD ....
set ANT_HOME=%~dp0%build\apache-ant-1.8.2
set ANT_BIN=%~dp0%build\apache-ant-1.8.2\bin
SET path=%path%;%ANT_BIN%;%ANT_BIN%;
echo PATH: %path%
echo ANT_HOME: %ANT_HOME%
echo ANT_BIN: %ANT_BIN%
echo ANT GOT INSTALLED ....
精彩评论