将文件从一个文件夹从文本文件的文件名列表移动到另一个文件夹

Sha*_*ron 2 powershell cmd command-prompt windows-7

我有一个文本文件,其中包含带有扩展名的文件名列表(文件名中没有空格)。 在此处输入图片说明

我在两个不同的路径中有两个文件夹,比如 C:\ThirdParty 和 C:\Users。这些路径中会有子文件夹。

我需要的?

我需要在这两个文件夹及其子文件夹的文本文件中搜索每个文件名,并将完全匹配的文件移动到另一个文件夹,例如 C:\Backup\ThirdParty 和 C:\Backup\Users。

如何在 Windows 7 中使用 powershell 或命令提示符执行此操作。

我不知道如何开始,或者我在 powershell 和 DOS 命令方面为零。

Rob*_*sso 7

此脚本可用于C:\ThirdParty并根据您的需要进行调整。

$file_list = Get-Content C:\list.txt
$search_folder = "C:\ThirdParty"
$destination_folder = "C:\Backup\ThirdParty"

foreach ($file in $file_list) {
    $file_to_move = Get-ChildItem -Path $search_folder -Filter $file -Recurse -ErrorAction SilentlyContinue -Force | % { $_.FullName}
    if ($file_to_move) {
        Move-Item $file_to_move $destination_folder
    }
}
Run Code Online (Sandbox Code Playgroud)

随着Get-Content您在名为$file_list.

$search_folder为在文件夹及其子文件夹中递归查找的每个文件设置一个循环。如果找到文件,则移动到$destination_folder

  • 值得注意的是,该脚本必须以 .ps1 扩展名保存并通过 Powershell 运行。 (2认同)