1

I have about 1,500 folders where the last 4 digits represent a year. I need to add parenthesis around the year while preserving the rest of the name. The folder names vary in length from about 4 characters to over 20. Some of the folder names contain numbers other than the date.

I found a way to put a parenthesis at the end of the folder name. As well as a way to add a parenthesis at a certain place from the beginning of the name. But cannot seem to find a way to count backward from the end of the name.

Get-ChildItem -Directory | Rename-Item -NewName {$_.Name -replace '^' ,")"}

Get-ChildItem -Directory  | % { $_ | Rename-Item -NewName ($_.Name -replace '^([\S\s]{5})', '$1(')}

I would like to take a folders like this:

Short.1990
Long.Folder.name.2019
Folder.1.1998

And end with folder names like this:

Short.(1990)
Long.Folder.name.(2019)
Folder.1.(1998)
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Mike
  • 13
  • 3
  • I would like to thank everyone who answered my question. This regular expression did the trick. – Mike Feb 08 '19 at 19:34

3 Answers3

2

Try the following -replace operation:

PS> 'Short.1990', 'Long.Folder.name.2019' -replace '\.(\d{4})$', '.($1)'
Short.(1990)
Long.Folder.name.(2019)

In the context of your command:

Get-ChildItem -Directory |
  Rename-Item -NewName { $_.Name -replace '\.(\d{4})$', '.($1)' } -WhatIf

Explanation of ... -replace '\.(\d{4})$', '.($1)':

  • Regex \.(\d{4})$ matches a literal . (escaped as \.), followed by 4 digits (\d{4}) at the end ($) of the input (file name).

    • Enclosing \d{4} in (...) creates a capture group that captures the specific digits matched, so they can be referred to in the replacement operand as $1 (the value of the 1st capture group).
  • Replacement expression '.($1)' replaces what the regex matched overall with a literal ., followed by the captured 4-digit sequence ($1) enclosed in literal (...).

  • For a comprehensive overview of the -replace operator, see this answer.

mklement0
  • 382,024
  • 64
  • 607
  • 775
1

The trailing .year in fact is an extension you could replace:

A one liner:

gci -dir|?{$_.Extension -match '^\.(\d{4})$'}|Ren -New {$_.Name -replace $_.Extension,(".({0})" -f $Matches[1])} -WhatIf

As a more verbose script:

Get-ChildItem -Directory | Where-Object {$_.Extension -match '^\.(\d{4})$'} |
    Rename-Item -Newname {$_.Name -replace $_.Extension,(".({0})" -f $Matches[1])} -WhatIf

Sample output (German locale):

WhatIf: Ausführen des Vorgangs "Verzeichnis umbenennen" für das Ziel "Element: A:\Folder.1.1998 Ziel: A:\Folder.1.(1998)".
WhatIf: Ausführen des Vorgangs "Verzeichnis umbenennen" für das Ziel "Element: A:\Long.Folder.name.2019 Ziel: A:\Long.Folder.name.(2019)".
WhatIf: Ausführen des Vorgangs "Verzeichnis umbenennen" für das Ziel "Element: A:\Short.1990 Ziel: A:\Short.(1990)".
0

Maybe not even need to use regex but just the following.. That is in case all folder contain a dot and end with the year!

$folders = Get-ChildItem "Path to you Share holding all your subfolders"
foreach($folder in $folders){
    try{
        $splitYear = $folder.Name.Split(".")[-1]
        $newName = $folder.Name.Replace($splitYear,"($splitYear)")
        Rename-Item -Path $folder.FullName -NewName $newName
        Write-Host "Changed name $($folder.name) to new name $newName" -ForegroundColor Green
    }catch{
        Write-Host "Couldn't change folder $($folder.Name), because $($_.Exception.Message)" -ForegroundColor Red
    }
}
Bernard Moeskops
  • 1,203
  • 1
  • 12
  • 16