Command Prompt - How to add a set path only for that batch file executing?

前端 未结 3 1613
北海茫月
北海茫月 2020-11-30 18:02

Basically, I know I can go through my control panel and modify the path variable. But, I\'m wondering if there is a way to through batch programming have a temporary path in

相关标签:
3条回答
  • 2020-11-30 18:09

    Just like any other environment variable, with SET:

    SET PATH=%PATH%;c:\whatever\else
    

    If you want to have a little safety check built in first, check to see if the new path exists first:

    IF EXIST c:\whatever\else SET PATH=%PATH%;c:\whatever\else
    

    If you want that to be local to that batch file, use setlocal:

    setlocal
    set PATH=...
    set OTHERTHING=...
    
    @REM Rest of your script
    

    Read the docs carefully for setlocal/endlocal , and have a look at the other references on that site - Functions is pretty interesting too and the syntax is tricky.

    The Syntax page should get you started with the basics.

    0 讨论(0)
  • 2020-11-30 18:09

    That's right, but it doesn't change it permanently, but just for current command prompt, if you wanna to change it permanently you have to use for example this:

    setx ENV_VAR_NAME "DESIRED_PATH" /m
    

    This will change it permanently and yes you can overwrite it by another batch script.

    0 讨论(0)
  • 2020-11-30 18:18

    There is an important detail:

    set PATH="C:\linutils;C:\wingit\bin;%PATH%"
    

    does not work, while

    set PATH=C:\linutils;C:\wingit\bin;%PATH%
    

    works. The difference is the quotes!

    UPD also see the comment by venimus

    0 讨论(0)
提交回复
热议问题