将多个文件/文件类型 - 从不同的文件夹(路径列表)复制到另一个文件夹

将多个文件/文件类型 - 从不同的文件夹(路径列表)复制到另一个文件夹

正如标题所述。

我在这里找到了这个: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 循环

@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

相关内容