Windows Batch Script Error - Quoting Variables Containing Spaces

So here is my problem: I want to use% cd% so that the user can execute the script anywhere he wants to place it, but if% cd% contains spaces, then it will fail (regardless of the quotes). If I set the path hard, it will work with quotes, but if it is a variable, it will fail.

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

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

Any ideas?

+3
source share
1 answer

%CD% - , , , script, , script.

%~dp0 , %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!
+3

Source: https://habr.com/ru/post/1740545/


All Articles