So I have hundreds of files in a folder:
The files are always First_Last (MMM yyyy):
C:\Test\Bob_Johnson (May 2017).pdf
C:\Test\James_Smith (Apr 2017).pdf
They get moved to individual folders based on their filenames (I think I know how to move the files). The folders are named Last, First 00-0000 where the zeros are the case number.
The following script will remove the (MMM yyyy) from the filenames:
Get-ChildItem -Path C:\Test | ?{ ($_.PsIsContainer)} | %{
$File = ($_.name.substring(0,$_.basename.length-11))
Write-Host $File
}
Output:
Bob_Johnson
James_Smith
Great. But it needs to be "Last, First".
Get-ChildItem -Path C:\Test | ?{!($_.PsIsContainer)} | %{
$File = ($_.BaseName -split '_')[1] + ", " + ($_.BaseName -split '_')[0]
Write-Host $File
}
Output:
Johnson (May 2017), Bob
Smith (Apr 2017), James
So here's my question: how do I combine the substring and the split so that it trims the last 11 characters and rearranges the first and last names with a ", " in between?
Desired Output:
Johnson, Bob
Smith, James