Windows批处理脚本问题 – 引用包含空格的variables

所以这是我的问题:我想使用%cd%,所以用户可以在任何地方执行脚本,但是如果%cd%包含空格,那么将会失败(不pipe引号是什么)。 如果我硬编码的path,它将引用function,但如果它是一个variables,它将失败。

失败:(如果%cd%包含空格)“%cd%\ Testing.bat”

Works:“C:\ Program Files \ Testing.bat”

有任何想法吗?

%CD%不是正确的方式,因为它表示调用脚本时用户所在的目录,而不是脚本所在的目录。

使用%~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!