【问题标题】:Powershell Move Files To Directory - Maintain Paths?Powershell 将文件移动到目录 - 维护路径?
【发布时间】:2019-11-02 18:59:36
【问题描述】:

尝试制作一个简单的备份脚本,如下所示:

移动以下

\source\example1.txt
\source\path\example2.txt

到

\dest\example1.txt
\dest\path\example2.txt

同时,重命名dest中已存在的所有文件。

我的代码:

$src = "C:\Users\User\Desktop\test1"
$dest = "C:\Users\User\Desktop\test2"

Get-ChildItem -Path $src -Filter *.txt -Recurse | ForEach-Object {
$num=1
    $nextName = Join-Path -Path $dest -ChildPath $_.name

    while(Test-Path -Path $nextName)
    {
       $nextName = Join-Path $dest ($_.BaseName + " ($num)" + $_.Extension)    
       $num+=1   
    }

    $_ | Move-Item -Destination $nextName
}

这几乎可以工作,但它将所有文件压缩到dest 中的一个文件夹中。 (\source\path\example.txt 变为 \dest\example.txt)

如何解决?

【问题讨论】:

    标签: powershell command-line terminal


    【解决方案1】:

    以下评论代码 sn-p 可以完成这项工作:

    Get-ChildItem -Path $src -Filter *.txt -Recurse | 
      ForEach-Object {
        $num=1
        # ChildPath                                  ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
        $nextName = Join-Path -Path $dest -ChildPath $_.FullName.Replace("$src\", '')
        # effective destination for current file
        $destNew  = $nextName | Split-Path
        # create effective destination silently if necessary
        if ( -not (Test-Path -Path $destNew) ) {
            $null = New-Item $destNew -ItemType Directory
        }
        while(Test-Path -Path $nextName)
        {
           #                     ↓↓↓↓↓↓↓↓
           $nextName = Join-Path $destNew ($_.BaseName + " ($num)" + $_.Extension)    
           $num+=1   
        }
        $_ | Move-Item -Destination $nextName
    }
    

    【讨论】:

      【解决方案2】:

      其他方法:

      function Get-ItemNameWithNumberIfExist($NewPath, $BaseName, $Extension, $rang)
      {
      
      #build a new file name
      if ($rang -eq 0)
      {
          $NewFileName="{0}{1}" -f $NewPath, $BaseName, $Extension
      }
      else
      {
          $NewFileName="{0}({1}){2}" -f $NewPath, $BaseName, $rang, $Extension
      }
      
      #build a new path file name (use combine for work on every SE)
      $NewPathFile=[System.IO.Path]::Combine($NewPath, $NewFileName)
      
      #recursive call if file exist
      if (Test-Path -Path $NewPathFile)
      {
          $rang++
          Get-ItemNameWithNumberIfExist $NewPath $BaseName $Extension $rang
      }
      else
      {
          $NewPathFile
      }
      
      } 
      
      $OldPath='C:\temp\tmp1\'
      $NewPath='C:\temp\tmp2\'
      
      Get-ChildItem $OldPath -file -Recurse | %{
      
      $NewPath=$_.DirectoryName.Replace($OldPath, $NewPath)
      
      #create directory without error if exist
      New-Item -ItemType Directory -Path $NewPath -Force
      
      #move item and rename if exist
      move-item $_.FullName (Get-ItemNameWithNumberIfExist $NewPath $_.BaseName $_.Extension 0)
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-06-06
        • 2021-11-16
        • 1970-01-01
        • 2020-12-30
        • 2013-04-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多