vote up 3 vote down star

So here's my issue: I want to use %cd% so a user can execute a script anywhere they want to place it, but if %cd% contains spaces, then it will fail (regardless of quotes). If I hardcode the path, it will function with quotes, but if it is a variable, it will fail.

Fails: (if %cd% contains spaces) "%cd%\Testing.bat"

Works: "C:\Program Files\Testing.bat"

Any ideas?

flag

1 Answer

vote up 2 vote down

%CD% is not the right way to do it, as it indicates the directory where the user was located when invoking the script, not the directory where the script resides.

Use %~dp0 instead to extract the drive and path information from %0:

REM C:\Program Files\test_caller.bat
@echo I am the caller and I reside in: "%~dp0"
@"%~dp0\test.bat"

...

REM C:\Program Files\test.bat
@echo Yippeee!

...

C:\>"\Program Files\test_caller.bat"
I am the caller and I reside in: "C:\Program Files\"
Yippeee!

C:\>e:

E:\>"C:\Program Files\test_caller.bat"
I am the caller and I reside in: "C:\Program Files\"
Yippeee!

Cheers, V.

link|flag

Your Answer

Get an OpenID
or
never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.