正如其他人指出的那样,您发布的代码无法运行。它当然不能在subfolder
和\
之间引入空格。
但是,如果您确实有一个变量在文件夹和/或文件名的末尾有不需要的空格,则很容易摆脱它们。
Windows不允许文件或文件夹名称的最后一个字符为点或空格 - 如果尝试创建它,Windows会从名称中去除尾随的点和空格。但是像DIR这样的命令不会忽略尾随空格。
D:\>mkdir "my folder "
D:\>dir "my folder "
Volume in drive D has no label.
Volume Serial Number is F8FD-5039
Directory of D:\my folder
File Not Found
D:\>dir "my folder"
Volume in drive D has no label.
Volume Serial Number is F8FD-5039
Directory of D:\my folder
03/29/2012 06:00 PM <DIR> .
03/29/2012 06:00 PM <DIR> ..
0 File(s) 0 bytes
2 Dir(s) 67,054,551,040 bytes free
您可以使用参数或FOR变量修饰符修剪路径名称中错误的尾点或空格。修饰符会将名称转换为标准格式,包括从路径中的每个文件夹名称中剥离尾随点和/或空格。
@echo off
set "myVar=my folder "
echo this will fail because of space at end of path
dir "%myVar%"
echo(
echo The ~f modifier strips the trailing space
for %%F in ("%myVar%") do dir "%%~fF"
这里是上面的脚本
this will fail because of space at end of path
Volume in drive D has no label.
Volume Serial Number is F8FD-5039
Directory of D:\my folder
File Not Found
The ~f modifier strips the trailing space
Volume in drive D has no label.
Volume Serial Number is F8FD-5039
Directory of D:\my folder
03/29/2012 06:00 PM <DIR> .
03/29/2012 06:00 PM <DIR> ..
0 File(s) 0 bytes
2 Dir(s) 67,054,551,040 bytes free
我敢肯定,你在这行的后面加上一个空格结果:'localDirectoryPath = d:/文件夹/ subfolder',只是将其删除! – Aacini 2012-03-29 23:48:10