正如其他人指出的那樣,您發佈的代碼無法運行。它當然不能在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