4

Suppose I have a folder with rar and pdf files for a group of people. Each person has a unique code, and he/she is associated with two files

Full name <unique code>_dummy text to remove.rar and some text <unique code>_another dummy text to remove.pdf

For example,

First Man 11e2345_some text to remove.rar and a person 11e2345_another text to remove.pdf

Second Lady 66t7899_remove this text.rar and different person 66t7899_dummy text to remove.pdf

So, I have two questions:

1- For the rar files, how to rename the files by removing all the text starting with the underscore after the unique code?

2- After renaming the rar files, how to give every pdf file the same name of its corresponding rar file by looking up the <unique code>?

My desired outcome should be

First Man 11e2345.rar and First Man 11e2345.pdf

Second Lady 66t7899.rar and Second Lady 66t7899.pdf


Update

If the unique code has letters and numbers like 14e0123 and I know its length (e.g. 7), how to edit this part of Karthick's code to adapt? since $ID = $_.BaseName -replace "[^0-9]", "" will remove all the letters before the lookup.

Get-ChildItem "*.rar" | % {

    $BaseName_rar = $_.BaseName
    # Find the ID by replacing all non-digit characters in BaseName string of the 'rar' file with empty string
    # This effectively returns the ID which are the only numbers expected in the filename 
    $ID = $_.BaseName -replace "[^0-9]", ""

    Get-ChildItem "*$ID.pdf"  | % { Rename-Item $_.FullName -NewName ($BaseName_rar + $_.Extension) }

}

2 Answers 2

3

The below piece of code should help you get through.

$sourceFolderPath = "D:\source"
Set-Location $sourceFolderPath

# 1 Replace the part of filename from underscore character with empty string
Get-ChildItem "*.rar", "*.pdf" | % { Rename-Item $_.Fullname -NewName (($_.BaseName -replace "_.*","") + $_.Extension) }

<# 2 ForEach 'rar' file object,
    - Get the ID
    - Get the corresponding 'pdf' by ID
    - Rename the 'pdf' with BaseName of 'rar'
#>
Get-ChildItem "*.rar" | % {

    $BaseName_rar = $_.BaseName
    # If ID is just numbers
    # Find the ID by replacing all non-digit characters in BaseName string of the 'rar' file with empty string
    # This effectively returns the ID which are the only numbers expected in the filename 
     # $ID = $_.BaseName -replace "[^0-9]", ""

    # UPDATE: If ID begins with a number and has a total length of 7
    $ID = & { 
        $_.BaseName -match "(\d.{6})" | Out-Null
        $matches[0] 
    }
    
    Get-ChildItem "*$ID.pdf"  | % { Rename-Item $_.FullName -NewName ($BaseName_rar + $_.Extension) }

}

UPDATE

Given that the ID begins with a digit and has a total length of 7, you can replace the $ID assignment statement to the below

$ID = & { 
        $_.BaseName -match "(\d.{6})" | Out-Null
        $matches[0] 
    }
1
  • I have one issue I forgot to mention: many times, the unique code has letters like 14e0123. So, what should be changed here?
    – Diaa
    Jun 12, 2020 at 12:09
2

Here's another approach that focuses on the delimiters in the text rather than length or character class. It assumes the underscore after the code is the first or only underscore in the filename.:

gci *.rar | ForEach{
   $NewBase = $_.BaseName.Split('_')[0]
   $Code    = $NewBase.Split(' ')[-1] 
   Rename-Item $_.FullName "$NewBase.rar"
   gci *.pdf | ? BaseName -match $Code | Rename-Item -NewName "$NewBase.pdf"
}

Step-by-step filename parsing demo:

PS C:\> $a = 'First Man 11e2345_some text to remove.rar'
PS C:\> $a.Split('_')
First Man 11e2345
some text to remove.rar
PS C:\> $a.Split('_')[0]
First Man 11e2345
PS C:\> $a.Split('_')[0].split(' ')
First
Man
11e2345
PS C:\> $a.Split('_')[0].split(' ')[-1]
11e2345
PS C:\>

References

Get-ChildItem/gci

ForEach-Object

String.Split Method

About Arrays

Rename-Item

Where-Object/?

PowerShell learning resources

2
  • Many thanks for this SBS demo. Would you suggest me some tutorial to learn the syntax of PowerShell by examples? For example, does [-1] mean that we get the last element in the array? Furthermore, I need to get myself familiar with the syntax, so I hope you can refer me to some tutorials.
    – Diaa
    Jun 13, 2020 at 0:38
  • 2
    Yes, ``[-1]` refers to the last element of an array. The onlilne documentation by MS is very good. I'll edit my answer to add some links. For a solid grasp of the fundamentals, get comfortable and watch Getting Started with Microsoft PowerShell Jun 13, 2020 at 19:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .