Method one: In batch, modify environment variables, once valid (that is, in the current script is valid)
Run in cmd
Set path==%path%;d:/mypath
Use set path to view the current environment variables
Method Two: Batch processing, modify the environment variables, permanently valid
:: Change the value of the PATH environment variable, add E:\tools
WMIC environment where "name= ' path ' and username= ' <system> '" Set variablevalue= "%path%;e:\tools"
Also introduce some of the use of wminc (simple and practical)
:: Get TEMP environment variable
WMIC environment where "name= ' temp '" Get username,variablevalue
:: Add system environment variable home, value is%homedrive%%homepath%
WMIC environment Create Name= "Home", Username= "<system>", variablevalue= "%homedrive%%homepath%"
:: Delete Home environment variable
WMIC environment where "name= ' home '" delete
:: Get TEMP environment variable
WMIC environment where "name= ' temp '" Get username,variablevalue
:: Change the value of the PATH environment variable, add E:\tools
WMIC environment where "name= ' path ' and username= ' <system> '" Set variablevalue= "%path%;e:\tools"
:: Add system environment variable home, value is%homedrive%%homepath%
WMIC environment Create Name= "Home", Username= "<system>", variablevalue= "%homedrive%%homepath%"
:: Delete Home environment variable
WMIC environment where "name= ' home '" delete
In general, the system variables that WMIC creates or modifies do not have to be restarted windows to take effect, but if you find that the detection does not take effect under DOS windows, you try to close the DOS window and test it again. It's in effect.
If your batch does not want to close and let the new variable directly to the lower program application can write this
View Plaincopy to Clipboardprint?
:: Check if there is no e:\tools in path (jump to run, do not proceed)
echo%path%|findstr/i "E:\tools" && (Goto Run)
:: Add first to prevent error when not modified
WMIC environment Create Name= "path", variablevalue= "e:\tools;%path%"
:: Modify again to prevent errors that have sometimes been added
WMIC environment where "name= ' path ' and username= ' <system> '" Set variablevalue= "e:\tools;%path%"
:: Re-apply instantly
Set "path=e:\tools;%path%"
: Run
Start program. exe
:: Check if there is no e:\tools in path (jump to run, do not proceed)
echo%path%|findstr/i "E:\tools" && (Goto Run)
:: Add first to prevent error when not modified
WMIC environment Create Name= "path", variablevalue= "e:\tools;%path%"
:: Modify again to prevent errors that have sometimes been added
WMIC environment where "name= ' path ' and username= ' <system> '" Set variablevalue= "e:\tools;%path%"
:: Re-apply instantly
Set "path=e:\tools;%path%"
: Run
Start program. exe
Batching, modifying the environment variable path method (plus environment variable)