我在子文件夹中有多个图像,我不知道有多少个文件夹。我想批处理脚本以查找所有文件夹中列出的名称,并将图像复制到目标文件夹。我试过下面的脚本但是找不到文件错误。
@echo off
rem Find files and copy files
setlocal EnableExtensions EnableDelayedExpansion
set "SourceBaseFolder=D:\System backup\picture batch file\Test\15oct2015"
set "TargetBaseFolder=C:\OutputFolder"
if not exist "%SourceBaseFolder%\*" (
echo %~nx0: There is no folder %SourceBaseFolder%
set "ErrorCount=1"
goto HaltOnError
)
cd /D "%SourceBaseFolder%"
if not exist "FileNames.txt" (
echo %~nx0: There is no file %SourceBaseFolder%\FileNames.txt
set "ErrorCount=1"
goto HaltOnError
)
set "ErrorCount=0"
for /F "usebackq delims=" %%N in ("FileNames.txt") do (
for /R %%J in ("%%N*") do (
set "FilePath=%%~dpJ"
if "!FilePath:%TargetBaseFolder%=!" == "!FilePath!" (
set "TargetPath=%TargetBaseFolder%\!FilePath:%SourceBaseFolder%\=!"
md "!TargetPath!" 2>nul
if exist "!TargetPath!\*" (
echo Copying file %%~fJ
copy /Y "%%~fJ" "!TargetPath!" >nul
) else (
set /A ErrorCount+=1
echo Failed to create directory !TargetPath!
)
)
)
)
:HaltOnError
if %ErrorCount% NEQ 0 (
echo.
pause
)
endlocal
任何人都可以解决这个问题吗?提前谢谢。
答案 0 :(得分:0)
您尝试上班的解决方案似乎过于复杂
我相信类似于以下内容应该做你想要的事情
FOR /F %%G IN ('dir /a-d /s /b "D:\System backup\picture batch file\Test\15oct2015"^|findstr /I /E /G:"D:\System backup\picture batch file\Test\15oct2015\FileNames.txt"') DO (
copy "%%G" "C:\OutputFolder"
)
为确保文件名匹配准确,filenames.txt中的所有文件名都应以反斜杠开头,例如
\filename1.file
\filename2.file
如有必要,您可以在批处理文件中轻松生成此类文件