Home > Software engineering >  How to bulk rename files to add a missing comma before the first space
How to bulk rename files to add a missing comma before the first space

Time:07-10

I have a folder with hundreds of files that are supposed to be to this standard:

Lastname, Firstname - 1010 ddmmyy.pdf

At least half of them are missing that comma, and I need it added to them all. Using something like this replaces all spaces with a comma, which is not what I need, and I'm stuck:

Get-ChildItem -Name *.pdf | Rename-Item -NewName { $_ -replace ' ',',' }

Is there a way to identify adding in a single comma at the beginning of the first blank space?

CodePudding user response:

You can use below to do that:

Get-ChildItem -Path 'D:\SomeWhere' -Filter '*.pdf' -File | Rename-Item -NewName { $_.Name -replace '^([^,\s] )\s (. )', '$1, $2' }

Regex details:

^               Assert position at the beginning of the string
(               Match the regular expression below and capture its match into backreference number 1
   [^,\s]       Match a single character NOT present in the list below
                The character “,”
                A whitespace character (spaces, tabs, line breaks, etc.)
                Between one and unlimited times, as many times as possible, giving back as needed (greedy)
)              
\s              Match a single character that is a “whitespace character” (spaces, tabs, line breaks, etc.)
                Between one and unlimited times, as many times as possible, giving back as needed (greedy)
(               Match the regular expression below and capture its match into backreference number 2
   .            Match any single character that is not a line break character
                Between one and unlimited times, as many times as possible, giving back as needed (greedy)
)

CodePudding user response:

Another regex solution based on the -replace operator, a streamlined version of Theo's helpful answer:

Get-ChildItem -Name *.pdf | 
  Rename-Item -NewName { $_.Name -replace '^[^, ] (?= )', '$&,' }

The basic idea is that if the word before the first space doesn't contain a comma, insert one right after it:

  • Regex:

    • ^[^, ] matches one or more ( ) characters (inside [...]) that are not (^) a , or a (space), at the start of the string (the first ^).

    • (?= ) is a look-ahead assertion that requires that a (space) follow what was matched before, without getting captured as part of the match.

  • Replacement operand:

    • $& refers to the substring that matched the whole regex, and following it with a , is what inserts the desired comma.

Note:

  • It is safe to unconditionally apply the -replace operation: if the file name doesn't match, the regex, it is returned as-is, and Rename-Item quietly ignores attempts to rename a file to the same name it already has.

  • Unfortunately, Rename-Item does not do that for directories and reports an error instead - see GitHub issue #14903.

CodePudding user response:

You could try filtering for all the .pdf that have a space in their name then adding a condition to check if the file already contains a , in it's Name, if it does skip and if it doesn't, replace the first space white space followed by everything else with a , plus all the replaced string, i.e.:

Get-ChildItem -Filter '* *.pdf' | ForEach-Object {
    if(-not $_.BaseName.Contains(',')) {
        $_ | Rename-Item -NewName { $_.Name -replace '\s. ', ',$0' }
    }
}
  • Related