How to set the working directory of a command in a Windows batch file?

前端 未结 3 816
庸人自扰
庸人自扰 2020-12-29 01:26

Let\'s say I have these commands:

Prog1.exe
D:\\SomeDir\\Prog2.exe
Prog3.exe

Now, say for the second line, I would like the working directo

相关标签:
3条回答
  • 2020-12-29 02:01

    You could use the pushd/popd commands (help with pushd /?)

    Prog1.exe
    Pushd D:\SomeDir
    Prog2.exe
    popd
    Prog3.exe
    
    0 讨论(0)
  • 2020-12-29 02:05

    You could use the cd command (help with cd /?) with the %~dp0, batch file path, variable.

    Prog1.exe
    cd D:\SomeDir
    Prog2.exe
    cd %~dp0
    Prog3.exe
    

    For a complete list of %~ modifiers see call /? or for /? help.

    However, I only add this as to provide a more complete answer on Stack Overflow. I would RECOMMEND using jeb's solution above.

    0 讨论(0)
  • 2020-12-29 02:09

    What worked for me is adding a /d:

    cd /d C:\nginx
    ECHO Stopping nginx...
    start nginx -s quit
    

    (When I didn't have the /d, it didn't work.)

    https://stackoverflow.com/a/18310141/470749 tries to explain it.

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