powershell 递归复制文件,排除列表中存在的文件

powershell copy files recursively excluding those that exist in a list

我有一个文件(例如 list.txt),其中包含文件路径列表,例如

F:\users\user\desktop\f1.doc
F:\users\user\desktop\f2.doc
F:\users\user\documents\f3.doc
F:\users\user\documents\f4.doc

我还在我的计算机上附加了一个 F: 驱动器,其中包含这些文件和其他未出现在 list.txt

中的文件

我想复制 F: 驱动器上不属于 list.txt 列表的所有文件,但保持文件夹结构不变。

类似于 "copy-item F:\users\user\desktop\f2a.doc -destination C:\copied\users\user\desktop\f2a.doc" 但对于 F: 驱动器中的所有文件。

我猜我将不得不使用 -filter,因为 -exclude 没有按预期工作,但我不知道如何使用。非常感谢任何帮助。

卡迈勒

这是我目前的代码:

$文件 = "list.txt"
$target = "c:\copied\"

$prevDir = "c:\dummy" $count = 0 foreach ($line in Get-Content $file){ $currDir = (Get-ChildItem $line).Directory if ($currDir -ne $prevDir) { $files = Get-ChildItem $currDir -Recurse foreach ($item in $files) { if ($item -ne (Get-ChildItem $fullPath).Name) { Copy-Item $item.FullName -Destination $target + $line $count++ } } } $prevDir = $currDir } </pre>

我最终用 C# 编写了一个小程序来完成此任务。任何对代码感兴趣的人都可以在这里找到它:https://github.com/ecofriend/CryptoFileSaver