【问题标题】:How to copy and rename a file in same directory?如何复制和重命名同一目录中的文件?
【发布时间】:2017-04-12 06:43:34
【问题描述】:

我的文件夹 C:\Downloads\Files\ 有 3 个 .zip 文件。

  • test1.zip
  • test2.zip
  • test3.zip

我需要将这些文件复制到同一位置,并将另一个扩展名 .dat 添加到文件中。

我的文件夹 C:\Downloads\Files\ 中的总文件现在应该有 6 个文件(3 个 .zip 和 3.dat)

  • test1.zip
  • test2.zip
  • test3.zip
  • test1.zip.dat
  • test2.zip.dat
  • test3.zip.dat

谁能帮我完成这项工作?

【问题讨论】:

    标签: powershell scripting


    【解决方案1】:

    使用Get-ChildItem cmdlet 检索所有压缩文件并使用Copy-Item cmdlet 复制它们:

    Get-ChildItem -Path 'C:\Downloads\Files\' -Filter '*.zip' | 
        Copy-Item -Destination { "$($_.Name).dat" }
    

    【讨论】:

      【解决方案2】:

      以下内容复制$path目录中扩展名为.dat的文件。

      Get-ChildItem -Path $path -File | ForEach-Object {Copy-Item -Path $_.FullName -Destination "$($_.FullName).dat"}
      

      【讨论】:

        猜你喜欢
        • 2021-12-05
        • 1970-01-01
        • 1970-01-01
        • 2014-09-01
        • 2020-12-28
        • 1970-01-01
        • 1970-01-01
        • 2012-01-27
        • 1970-01-01
        相关资源
        最近更新 更多