考虑我有一个包含文件列表
的文件input.txt
input.txt
img001.img,
doc001.docx,
doc002.docx,
csv001.csv,
pdf001.pdf,
csv002.csv
@echo off
del /f Dirfilelisttemp.txt
rd DirTempList
echo ***** READ Started,Input.txt *****
for /f "tokens=*" %%a in (input.txt) do (
dir /s /b D:\com\example_4.1\%%a>>Dirfilelisttemp.txt
)
echo ***** READ COMPLETED Input.txt *****
echo ***** Render Started,Dirfilelisttemp.txt *****
for /f "tokens=*" %%a in (Dirfilelisttemp.txt) do (
xcopy /e /v /s %%a" /-y D:\DirTempFolder
)
echo *****Render COMPLETED,Dirfilelisttemp.txt *****
注意:Dirfilelisttemp.txt
d:\ COM \ example_4.1 \ IMG \ img001.img,
d:\ COM \ example_4.1 \文档\ doc001.docx,
d:\ COM \ example_4.1 \文档\ doc002.docx,
d:\ COM \ example_4.1 \ CSV \ csv001.csv,
d:\ COM \ example_4.1 \ CSV \ csv002.csv
d:\ COM \ example_4.1 \ CSV \ pdf001.pdf
所需的输出:
d:\ DirTempFolder \ COM \ example_4.1 \ IMG \ img001.img,
d:\ DirTempFolder \ COM \ example_4.1 \文档\ doc001.docx,
d:\ DirTempFolder \ COM \ example_4.1 \文档\ doc002.docx,
d:\ DirTempFolder \ COM \ example_4.1 \ CSV \ csv001.csv,
d:\ DirTempFolder \ COM \ example_4.1 \ CSV \ csv002.csv
d:\ DirTempFolder \ COM \ example_4.1 \ CSV \ pdf001.pdf
实际输出:
d:\ DirTempFolder \ img001.img,
d:\ DirTempFolder \ doc001.docx,
d:\ DirTempFolder \ doc002.docx,
d:\ DirTempFolder \ csv001.csv,
d:\ DirTempFolder \ pdf001.pdf,
d:\ DirTempFolder \ csv002.csv
执行此操作后,我将获得“实际输出”。但我无法获得所需的输出。 是他们获得所需输出的任何解决方案吗?
答案 0 :(得分:0)
您只需要对上一个for循环进行一些简单的更改,然后添加delayedexpansion
@echo off
setlocal enabledelayedexpansion
if exist Dirfilelisttemp del /f /q Dirfilelisttemp.txt
if exist DirTempList rd /s /q DirTempList
for /f "tokens=*" %%a in (input.txt) do (
dir /s /b "D:\com\example_4.1\%%a">>Dirfilelisttemp.txt
)
for /f "tokens=*" %%a in (Dirfilelisttemp.txt) do (
set "ext=%%~xa" & set ext=!ext:,=!
if not exist "c:\Temp%%~pa" md "c:\Temp%%~pa"
xcopy /e /v /s "%%~dpna!ext!" /-y "D:\DirTempFolder%%~pa"
)
您没有指定删除文件名末尾的逗号,但我怀疑您在扩展名中有这些逗号,所以我删除了它们。
答案 1 :(得分:0)
@echo off
setlocal enableextensions
set "sourceDir=D:\com\example_4.1"
set "targetDir=D:\DirTempFolder"
for /f "usebackq tokens=*" %%a in ("input.txt"
) do for %%b in ("%sourceDir%\%%~a"
) do if exist "%%~fb" xcopy /y "%%~fb" "%targetDir%%%~pb"
endlocal