正如标题所述。
我在这里找到了这个:https://stackoverflow.com/a/1294140
set dSource=C:\tmp\Aufgaben\Tom VPKs
set dTarget=C:\tmp
set fType=*.eps
for /f "delims=" %%f in ('dir /a-d /b /s "%dSource%\%fType%"') do (
copy /V "%%f" "%dTarget%\" 2>nul
)
我现在需要的是:(设置 dSource=C:\tmp\Aufgaben\Tom VPKs) 但使用列表作为输入。
"M:\z Grafikdesign\articles\44329"
"M:\z Grafikdesign\articles\24850"
"M:\z Grafikdesign\articles\26513"
"M:\z Grafikdesign\articles\27256"
"M:\z Grafikdesign\articles\29312"
"M:\z Grafikdesign\articles\30902"
"M:\z Grafikdesign\articles\41285"
"M:\z Grafikdesign\articles\42242"
"M:\z Grafikdesign\articles\42790"
"M:\z Grafikdesign\articles\43665"
"M:\z Grafikdesign\articles\43931"
"M:\z Grafikdesign\articles\44251.2"
像这样。
一个很大的优点是对正确答案的解释:)
答案1
FOR 循环
- FOR 循环从文本文件中获取目录列表
- 笔记:准备文本文件不带引号围绕目录
- 另一个 FOR 循环查找所提供目录中的所有文件
- 使用
usebackq
(文档) 来处理文件名中的空格(例如“Tom VPKs”)。 - 参见 SO”如何使用 Windows 批处理文件循环遍历文本文件中的每一行?“
@echo off
set "dTarget=C:\tmp"
set "dSource=C:\Path\to\a text\file.txt"
set "fType=.eps"
rem loop once for each directory in %dSource%
for /F "usebackq tokens=*" %%D in ("%dSource%") do (
rem loop once for each file in %%D and its subdirectories
for /F "tokens=* delims= " %%F in ('Dir "%%D" /A:-D /b /s') do (
rem copy only if %%F's extension matches %fType%
if "%%~xF" == "%fType%" (
copy /V "%%F" "%dTarget%\" 2>nul
)
)
)
答案2
如果电源外壳是一个选项,此代码将源路径数组与扩展数组组合起来,然后对所有结果路径执行复制操作:
$Target = 'C:\tmp'
$Source = @('M:\z Grafikdesign\articles\44329'
'M:\z Grafikdesign\articles\24850'
'M:\z Grafikdesign\articles\26513'
'M:\z Grafikdesign\articles\27256'
'M:\z Grafikdesign\articles\29312'
'M:\z Grafikdesign\articles\30902'
'M:\z Grafikdesign\articles\41285'
'M:\z Grafikdesign\articles\42242'
'M:\z Grafikdesign\articles\42790'
'M:\z Grafikdesign\articles\43665'
'M:\z Grafikdesign\articles\43931'
'M:\z Grafikdesign\articles\44251.2'
)
$Types = @('.txt', '.log', '.bat')
# Create [String[]] of paths with wildcard basename
$Paths = ForEach ($Type in $Types)
{
Join-Path $Source ('*' + $Type)
} | Sort
# $Paths
Copy-Item $Paths $Target