change directory in batch file using variable

喜夏-厌秋 提交于 2020-06-09 08:39:34

问题


Here's the question:

set Pathname = C:\Program Files
cd %Pathname%
pause

The above doesn't change the directory, as I would expect. Can anybody please tell me why?


回答1:


The set statement doesn't treat spaces the way you expect; your variable is really named Pathname[space] and is equal to [space]C:\Program Files.

Remove the spaces from both sides of the = sign, and put the value in double quotes:

set Pathname="C:\Program Files"

Also, if your command prompt is not open to C:\, then using cd alone can't change drives.

Use

cd /d %Pathname%

or

pushd %Pathname%

instead.




回答2:


simple way to do this... here are the example

cd program files
cd poweriso
piso mount D:\<Filename.iso> <Virtual Drive>
Pause

this will mount the ISO image to the specific drive...use



来源:https://stackoverflow.com/questions/4133244/change-directory-in-batch-file-using-variable

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