Rename Files to include Parent & Grantparent folde

2019-09-10 09:56发布

问题:

I have folders that follow the pattern like this:

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

I would like to rename the pdfs to something like root folder-grandparent folder-parent folder.1.pdf and root folder-grandparent folder-parent folder.2.pdf etc. and if possible move this file up to the root folder level.

I found this powershell script that does something similar but it only takes the parent folder name.

This is what i have:

#######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

回答1:

Got light from @Joye 's code below. I tested Joye's code it just gave me "given format not supported" error. Not sure this is my lab or not (Powershell V3). Then I modified it a little it works in my lab:

 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:

If they all follow the same pattern and there are no files in grandparent or root, then that's pretty trivial:

$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:

To get the Great Grandparent simply add:

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))
}

This really helped me. Also thanks to Peters modified script which Joye originally created.