change directory in batch file using variable

前端 未结 2 1504
独厮守ぢ
独厮守ぢ 2021-02-01 03:57

Here\'s the question:

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

The above doesn\'t change the directory, as I would expect. Can anyb

2条回答
  •  闹比i
    闹比i (楼主)
    2021-02-01 04:19

    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.

提交回复
热议问题