33

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?

2 Answers 2

63

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Probably worth mentioning that the quotes aren't in general required to use SET with a string containing spaces. SET C:\Program Files would also have worked.
@MonkeyPushButton, I think you meant set Pathname=C:\Program Files, but yeah, it does work with spaces
It depends on the command. cd and pushd behave nicely with or without quotes around the path. md, rd and copy don't, to name a few. I generally put quotes around all paths that use spaces, just to be safe.
The /d was needed for me since I was CD'ing to a different drive EG, cd from C: to G: required /d
-6

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

1 Comment

This does not answer the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.