How to change current working directory using a batch file

泄露秘密 提交于 2019-11-27 19:51:19

问题


I need some help in writing a batch file. I have a path stored in a variable root as follows:

set root=D:\Work\Root

Then I am changing my working directory to this root as follows:

cd %root%

When I execute this batch file from anywhere on the D drive this is done successfully. But when I execute the same batch file from some other drive, cd %root% doesn't work.

Is there a way I can get the drive letter from the root variable? I can then change the current directory to this drive first and then cd %root% shall work.


回答1:


Specify /D to change the drive also.

CD /D %root%



回答2:


Just use cd /d %root% to switch driver letters and change directories.

Alternatively, use pushd %root% to switch drive letters when changing directories as well as storing the previous directory on a stack so you can use popd to switch back.

Note that pushd will also allow you to change directories to a network share. It will actually map a network drive for you, then unmap it when you execute the popd for that directory.




回答3:


Try this

chdir /d D:\Work\Root

Enjoy rooting ;)




回答4:


A simpler syntax might be

pushd %root%



来源:https://stackoverflow.com/questions/5138507/how-to-change-current-working-directory-using-a-batch-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!