Powershell script to change mutliple file names each with unique name to a standard format

alejansalin
alejansalin used Ask the Experts™
on
I am seeking a script that will do the following:

Take a file folder and rename it to a common standard across the entire organization.
Take each file within a folder and rename it to a common standard using the existing file name.

I actually have the file folder portion completed, as the powershell

Get-ChildItem -Filter "*current*" -Recurse | Rename-Item -NewName {$_.name -replace 'current','old' }

However, the issue is that the file names used are all over the board.  Examples, amongst a horde of others:

Christopher, Jack 4.2014
Christopher, Jack 042014
Christopher Jack 04-02-2014
Jack Christopher April 2014

What I would like to do is implement a standard of:

JChristopher 042014

So the script should look at the entire file name and then rename it accordingly.

Yes.  There are subfolders sometimes 4 layers deep in some instances, and I can address that separately.

However, there are literally 8K files (not file size), but files that need to meet this new standard.

It's a non-profit who were erroneously informed that they did NOT have to meet HIPPA requirements even though they work with PHI of SP adults.

I know this should be simple, with everything else that is going on, I figured it wouldn't hurt to just ask.  Perhaps someone has already encountered something similar?

Server 2012R2, workstations all running W10Pro.

Thanks,

René
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2014

Commented:
Does the date pattern correlate to the firstname/lastname relationship for that line?

In your example, the only line in firstname lastname order has a date of April 2014
All the other date formats are in lastname firstname order.
Chirag NagrekarAssociate - Sys Admin

Commented:
Christopher, Jack 4.2014
Christopher, Jack 042014
Christopher Jack 04-02-2014
Jack Christopher April 2014

These are only 4 formats of file name ? If there are many like this then it is very difficult (almost impossible) to rename files because you don't know how many conditions you want to put ?

Second scenario if your file names are in this 4 formats only then you can apply multiple conditions and then rename.

Third is if your files are corresponding to user related folder then you can rename files according to username with your mentioned format.

We can guess many multiple possibilities here but thing is how we can sort this out simply.
Top Expert 2014

Commented:
Here's a proof of concept PS script you can play with.  Add additional file names to the array at the top.  Add cases to the switch statement.
NOTE:  I've left the commented write-host statements to help with your testing.
$a = @('Christopher, Jack 4.2014','Christopher, Jack 042014',
       'Christopher Jack 04-02-2014','Jack Christopher April 2014')
       
$a | %{
    switch -regex ($_){
        "^([^, ]+),? ([^ ]+) (\d\.\d{4})$"{
                #write-host "LN:"$matches[1],
                #"FN:"$matches[2][0],
                #"Dt:"$(("0", $($matches[3][0]), ($matches[3][-4..-1] -join '')) -join '') ;
                $newname = $matches[2][0]+$matches[1]+$(("0", $($matches[3][0]), ($matches[3][-4..-1] -join '')) -join '')
                write-host $newname
                break}
        "^([^, ]+),? ([^ ]+) (\d{6})$"{
                #write-host "LN:"$matches[1],
                #"FN:"$matches[2][0],
                #"Dt:"$matches[3];
                $newname = $matches[2][0]+$matches[1]+$matches[3]
                write-host $newname
                break}
        "^([^, ]+),? ([^ ]+) (\d\d-\d\d-\d{4})$"{
                #write-host "LN:"$matches[1],
                #"FN:"$matches[2][0],
                #"Dt:"(($matches[3][0..1] -join ''), ($matches[3][-4..-1] -join '') -join ''); 
                $newname = $matches[2][0]+$matches[1]+(($matches[3][0..1] -join ''), ($matches[3][-4..-1] -join '') -join '')
                write-host $newname
                break}
        "^([^ ]+) ([^ ]+) (\w+ \d{4})$"{
                #write-host "LN:"$matches[2],
                #"FN:"$matches[1][0],
                #"Dt:"([datetime]$matches[3]).tostring("MMyyyy"); 
                $newname = $matches[1][0]+$matches[2]+([datetime]$matches[3]).tostring("MMyyyy")
                write-host $newname
                break}
        default {write-host "not matched"; break}
        }
        
    }

Open in new window

Top Expert 2014

Commented:
@alejansalin

Have you abandoned your question?

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial