【问题标题】:Rename Files to include Parent & Grantparent folder - powershell重命名文件以包括父和祖父文件夹 - powershell
【发布时间】:2017-06-27 22:50:38
【问题描述】:

我的文件夹遵循这样的模式:

C:\root folder\grandparent folder\parent folder\00001.pdf
C:\root folder\grandparent folder\parent folder\00002.pdf

我想将 pdf 重命名为 root folder-grandparent folder-parent folder.1.pdf 和 root folder-grandparent folder-parent folder.2.pdf 等,如果可能的话将这个文件移到根目录文件夹级别。

我发现这个 powershell 脚本做了类似的事情,但它只使用父文件夹名称。

这就是我所拥有的:

#######Rename script#############

$path = Split-Path -parent $MyInvocation.MyCommand.Definition 

Function renameFiles 
{ 
  # Loop through all directories 
  $dirs = dir $path -Recurse | Where { $_.psIsContainer -eq $true } 
  Foreach ($dir In $dirs) 
  { 
    # Set default value for addition to file name 
    $i = 1 
    $newdir = $dir.name + "_" 
    # Search for the files set in the filter (*.pdf in this case) 
$files = Get-ChildItem -Path $dir.fullname -Filter *.pdf -Recurse 
Foreach ($file In $files) 
{ 
  # Check if a file exists 
  If ($file) 
  { 
    # Split the name and rename it to the parent folder 
    $split    = $file.name.split(".pdf") 
    $replace  = $split[0] -Replace $split[0],($newdir + $i + ".pdf") 

    # Trim spaces and rename the file 
    $image_string = $file.fullname.ToString().Trim() 
    "$split[0] renamed to $replace" 
    Rename-Item "$image_string" "$replace" 
    $i++ 
      } 
    } 
  } 
} 
# RUN SCRIPT 
renameFiles

【问题讨论】:

    标签: powershell rename


    【解决方案1】:

    从下面@Joye 的代码中得到启发。我测试了乔伊的代码,它只是给了我“不支持给定格式”的错误。不确定这是否是我的实验室(Powershell V3)。然后我对其进行了一些修改,它可以在我的实验室中使用:

     get-childitem C:\root folder\grandparent folder\parent folder\*.pdf |
        % {
    
        $ParentOjbect =$_.Directory
        $Parent =$ParentOjbect.Name
        $GrandParent = $ParentOjbect.Parent
    
        Move-item $_ -Destination (Join-Path C:\root folder ('{0}{1}{2}' -f $GrandParent,$Parent,$_.Name))
        }
    

    【讨论】:

      【解决方案2】:

      如果它们都遵循相同的模式并且祖父母或根目录中没有文件,那么这很简单:

      $root = 'C:\root folder'
      
      Get-ChildItem -Recurse |
        ForEach-Object {
          $parent = $_.Parent
          $grandparent = $parent.Parent
          $number = [int]$_.BaseName
      
          Move-Item $_ -Destination (Join-Path $root ('{0}-{1}-{2}{3}' -f $grandparent, $parent, $number, $_.Extension))
        }
      

      【讨论】:

        【解决方案3】:

        要获得曾祖父母,只需添加:

        Get-childitem "Y:\DIR\*" -include *.log -recurse |
        % {
        
        $nextName = Join-Path -Path 'Y:\DIR\*' -ChildPath $_.name
        
        while(Test-Path -Path $nextName)
        {
           $nextName = Join-Path Y:\DIR ($_.BaseName + "_$num" + $_.Extension)    
           $num+=1   
        }
        
        $ParentOjbect =$_.Directory
        $Parent =$ParentOjbect.Name
        $GrandParent = $ParentOjbect.Parent
        $GreatGran = $GrandParent.Parent
        
        Copy-item $_ -Destination (Join-Path "Y:\DIR" ('{0}_{1}_{2}_{3}' -f $GreatGran,$GrandParent,$Parent,$_.Name))
        }
        

        这对我很有帮助。还要感谢 Peters 修改了 Joye 最初创建的脚本。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-10-07
          • 1970-01-01
          • 2014-08-10
          • 2020-04-30
          相关资源
          最近更新 更多