3

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?

1 Answer 1

3

%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!
Sign up to request clarification or add additional context in comments.

Comments

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.