【问题标题】:Move Powershell File Structure if no File exists in Target Directory如果目标目录中不存在文件,则移动 Powershell 文件结构
【发布时间】:2018-07-17 10:32:33
【问题描述】:

我想将由 .zip 文件解压缩的文件结构移动到目标目录。

解压 ZIP 有效,文件移动也有效。

但在某些情况下,目标目录已经包含与要复制的文件结构同名的文件。

也许我想将所有文件从 /temp/ 复制到 /target/

有一个文件 /temp/new/2/test.txt 并且它已经存在于 /target/new/2/test.txt 中

=> 在这种情况下,我不想将任何文件从临时文件夹复制到目标文件夹。

如何使用 powershell 实现这一点?

我现在移动文件的方式是:

Get-ChildItem -Path $unzipDir -Recurse | Move-Item -Destination $targetDir

谢谢你:)

【问题讨论】:

    标签: file powershell


    【解决方案1】:

    过滤掉目标路径已经存在的文件。

    Get-ChildItem -Path $unzipDir -Recurse |
    Where { -not Test-Path (Join-Path $targetDir $_) } |
    Move-Item -Destination $targetDir
    
    • Where 正文中,$_ 指的是当前文件。
    • Join-Path 处理从两部分构建有效路径。

    【讨论】:

    • 非常感谢 :) 不是我想要的 100%,但我可以过滤,哪些文件存在,如果文件存在,我可以分解文件的移动。谢谢!
    • 啊,那我误解了你的意思(忽略了“任何”)。很好,无论如何你都可以使用它。
    【解决方案2】:
    if(-not (Test-Path "./target/new/2/test.txt")){
        Get-ChildItem -Path $unzipDir -Recurse | Move-Item -Destination $targetDir
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-05
      • 2018-10-09
      • 1970-01-01
      • 2017-03-15
      • 2013-05-26
      • 1970-01-01
      • 2014-06-06
      相关资源
      最近更新 更多