我对编码一窍不通,所以请原谅我使用的术语可能不太准确!
我从网上选了一些批处理文件的代码(其中有些来自这个网站),并尝试重写其中的一部分以适应我的需求。
它创建一个新目录,然后查找另一个目录及其子目录中在 .txt 列表中的文件名,并将这些文件复制到最初创建的目录中。任何未在列表中找到的文件都会被写入另一个文本文件,以跟踪缺失的内容。
代码如下:
md 00FoundImages
set LIST=%cd%\imagelist.txt
set FILESPATH=%cd%\testimages
set DEST=%cd%\00FoundImages
for /f "delims=" %%x in (%LIST%) do (forfiles /p %FILESPATH% /s /m %%x* /c "cmd /c move /y @path %DEST%\@file" 2>>errors.txt)
在大多数情况下,这样做运行良好,但是当以下条件出现时会出现问题:
- 文本文件中的文件名包含空格
- 批处理文件所在目录的路径包含空格(或其他特殊字符)
我尝试过使用引号括起来,如下所示:
set FILELIST="%cd%\imagelist.txt"
set FILESPATH="%cd%\testimages"
set DESTPATH="%cd%\00FoundImages"
当目录名中有空格时,forfiles
命令会失败。
我尝试在变量使用时用引号括起来,如下所示:
for /f "delims=" %%x in ("%LIST%") do (forfiles /p "%FILESPATH%" /s /m %%x* /c "cmd /c move /y @path "%DEST%\@file"" 2>>errors.txt)
当我从名为“testing-Copy”的目录运行批处理文件时,错误会被写入我的errors.txt文件中。
ERROR: Invalid argument/option - '-'.
Type "FORFILES /?" for usage.
这是正在发生的打印内容:
E:\ImageAutomationStuff\testing - Copy>md 00FoundImages
A subdirectory or file 00FoundImages already exists.
E:\ImageAutomationStuff\testing - Copy>set LIST=E:\ImageAutomationStuff\testing - Copy\imagelist.txt
E:\ImageAutomationStuff\testing - Copy>set FILESPATH=E:\ImageAutomationStuff\testing - Copy\testimages
E:\ImageAutomationStuff\testing - Copy>set DEST=E:\ImageAutomationStuff\testing - Copy\00FoundImages
E:\ImageAutomationStuff\testing - Copy>pause
Press any key to continue . . .
E:\ImageAutomationStuff\testing - Copy>for /F "delims=" %x in ("E:\ImageAutomationStuff\testing - Copy\imagelist.txt") do (forfiles /p "E:\ImageAutomationStuff\testing - Copy\testimages" /s /m %x* /c "cmd /c move /y @path "E:\ImageAutomationStuff\testing - Copy\00FoundImages\@file"" 2>>errors.txt )
E:\ImageAutomationStuff\testing - Copy>(forfiles /p "E:\ImageAutomationStuff\testing - Copy\testimages" /s /m E:\ImageAutomationStuff\testing - Copy\imagelist.txt* /c "cmd /c move /y @path "E:\ImageAutomationStuff\testing - Copy\00FoundImages\@file"" 2>>errors.txt )
E:\ImageAutomationStuff\testing - Copy>pause
Press any key to continue . . .
从我所做的研究中,我可以看到这类问题经常被问到 - 很抱歉增加了困扰,但由于我的知识有限,我恐怕无法自行解决此问题!
set
命令中的引号。它应该在屏幕上打印出它正在做什么(这里没有人拥有与你相同的文件结构)。复制并粘贴那段代码。如果批处理中有Echo Off
,请用 (rem
表示注释)把它注释掉。 - user6017774set "FILELIST=%cd%\imagelist.txt"
- phuclv