【问题标题】:Move files from one folder to another from a list of filenames from a text file从文本文件的文件名列表中将文件从一个文件夹移动到另一个文件夹
【发布时间】:2017-08-08 08:53:15
【问题描述】:

我有一个文本文件,其中包含带有扩展名的文件名列表(文件名中没有空格)。

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

我需要什么?

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

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

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

【问题讨论】:

    标签: powershell cmd windows-7 command-prompt


    【解决方案1】:

    此脚本可用于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
        }
    }
    

    使用Get-Content,您可以在名为$file_list 的数组中检索文件列表(每行一个)。

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

    【讨论】:

    • 值得注意的是,此脚本必须以 .ps1 扩展名保存并通过 Powershell 运行。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多