【问题标题】:Move Files From One Location To Another And Exclude Subfolders将文件从一个位置移动到另一个位置并排除子文件夹
【发布时间】:2018-10-05 19:10:16
【问题描述】:

我想将文件从一个位置移动到另一个位置,但不想从要从中移动文件的目录中的子文件夹中移动任何文件。

我有以下代码,但这会将文件移动到子文件夹中。此外,扩展名只能是 .xml 和 .pdf。

此脚本将所有文件从$MoveThese 移动到目标,甚至包括子文件夹中的文件,例如C:\Test\Folder1\Subfolder。任何建议都会有所帮助。

$MoveThese = "C:\Test\Folder1", "C:\Test2\Folder2", "C:\Test3\Folder3"
$Destination = "C:\Test\Docs", "C:\Test2\Docs", "C:\Test3\Docs"

For($i=0; $i -lt $FailFolders.Length; $i++){
Get-ChildItem -Path $MoveThese[$i] -Recurse -Include "*.xml", "*.pdf" | Move-Item -Destination $Destination[$i] 
}

【问题讨论】:

    标签: powershell


    【解决方案1】:

    如果您不希望子文件夹中的文件,请不要使用-Recurse。根据Get-ChildItem documentation,此“获取指定位置中的项目以及位置的所有子项目。”


    错过了-Include 声明的这一部分:

    -Include 参数仅在命令包含 -递归参数

    解决方案:

    Get-ChildItem  $MoveThese[$i] -File |
        Where-Object {$_.Extension -in @(".xml",".pdf")} |
        Move-Item -Destination $Destination[$i]
    

    【讨论】:

    • 当我删除 -Recurse 时,这不会移动 $MoveThese 位置中的任何项目。
    • -Include 不再工作 - 请参阅编辑
    【解决方案2】:

    不幸的是,如果没有 -Recurse 开关,除非路径使用像 C:\Windows* 这样的通配符,否则包含将不起作用

    这可能对你有用:

    $MoveThese = "C:\Test\Folder1", "C:\Test2\Folder2", "C:\Test3\Folder3"
    $Destination = "C:\Test\Docs", "C:\Test2\Docs", "C:\Test3\Docs"
    
    For($i=0; $i -lt $MoveThese.Length; $i++){
      Get-ChildItem  $MoveThese[$i] -File | 
        Where-Object { $_.Name -like "*.xml" -or $_.Name -like "*.pdf" | 
        Move-Item -Destination $Destination[$i] -Force
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多