Sorting by substring within filename

Please advise me on how to utilize the scripting capabilities within POWERSHELL to sort the following list of files by the date stored between the underscores? Once sorted how can I go about renaming the files "MMDDYYYY_MP###.##_Inspection Report,xls"

MP1018.78_04292014_Inspection Report.xls
MP1018.78_04302014_Inspection Report.xls
MP1043.21_04162013_Inspection Report.xls
MP1043.21_04172013_Inspection Report.xls
MP1043.21_04182013_Inspection Report.xls
MP1043.21_04222013_Inspection Report.xls
MP1043.21_04232013_Inspection Report.xls
MP1043.21_04242013_Inspection Report.xls
MP1043.21_04252013_Inspection Report.xls
MP1043.21_04262013_Inspection Report.xls
MP1043.21_04272013_Inspection Report.xls
MP1043.21_04282013_Inspection Report.xls
MP1043.21_04292013_Inspection Report.xls
MP1043.21_05012013_Inspection Report.xls
MP1043.21_06012013_Inspection Report.xls
MP793 65_03122014_Inspection Report.xlsx
MP793 65_03132014_Inspection Report.xlsx
MP793.65_03102014_Inspection Report.xlsx
MP793.65_03112014_Inspection Report.xlsx
MP800.32_02242014_Inspection Report.xlsx
MP800.32_02252014_Inspection Report.xlsx
MP800.32_02282014_Inspection Report.xlsx
MP806.42_02172014_Inspection Report.xls
MP806.42_02182014_Inspection Report.xls
MP806.42_02192014_Inspection Report.xls
MP806.42_02202014_Inspection Report.xlsx
MP806.67_03062014_Inspection Report.xls
MP806.70_03052014_Inspection Report.xls
MP806.70_03052014_Inspection Report.xlsx
MP853.78_04012013_Inspection Report.xls
MP857.76_08112014_Inspection Report.xlsx
MP857.76_08122014_Inspection Report.xlsx
MP857.76_08132014_Inspection Report.xlsx
MP857.76_08142014_Inspection Report.xlsx
MP857.76_08152014_Inspection Report.xlsx
MP857.76_08162014_Inspection Report.xlsx
MP857.76_08172014_Inspection Report.xlsx
MP864.61_04022013_Inspection Report.xls
MP864.61_04032013_Inspection Report.xls
MP883.91_04022013_Inspection Report.xls
MP895.94_03042014_Inspection Report.xlsx
MP903.41_02262014_Inspection Report.xlsx
MP919.51_04102013_Inspection Report.xls


March 20th, 2015 9:55am

As you didn't post any existing script, you can use something like this to get you started.  You'll need to change it to work with the get-item/get-childitem that you're using to generate the file listing instead of a static array of names, and in the final loop change the write-host to actually rename the files using the move-item cmdlet.

$filenames = @('MP1018.78_04292014_Inspection Report.xls',
        'MP1018.78_04302014_Inspection Report.xls',
        'MP806.42_02192014_Inspection Report.xls',
        'MP1043.21_04282013_Inspection Report.xls',
        'MP895.94_03042014_Inspection Report.xlsx')

$newOrder=@()

foreach ($file in $filenames) {
    $date = $file.split("_")[1]
    $newOrder+=[pscustomobject][ordered]@{
        'Date' = get-date -Year $date.Substring(4,4) -Month $date.substring(0,2) -Day $date.substring(2,2)
        'Filename' = $file
        'NewFilename' = "$($date)_$($file.Split("_")[0])_$($file.Split("_")[2])"}
}
$newOrder = $newOrder | sort Date
foreach ($order in $newOrder) {
    write-host "Change file $($order.filename) to $($order.newfilename)"
}


  • Edited by kdcooke 13 hours 13 minutes ago
Free Windows Admin Tool Kit Click here and download it now
March 20th, 2015 1:56pm

Other method:

[datetime]$date=0 $files=import-csv filenames.txt -header filename,date, newname $files | %{ if($_.filename -match '_(.*)_'){ $d=$matches[1] [void][DateTime]::TryParseExact($d, "MMddyyyy", [System.Globalization.CultureInfo]::InvariantCulture, [System.Globalization.DateTimeStyles]::None, [ref]$date) $_.Date=$date
$f=$_.filename -split '_'
            $_.NewName='{0}_{1}_{2}' -f $f[1],$f[0],$f[2] } }
$files | sort date

March 20th, 2015 4:19pm

If you want the files to sort in the folder then change the date format tp 'yyyyMMdd' which wil sort correctly in explorer.

Free Windows Admin Tool Kit Click here and download it now
March 20th, 2015 4:30pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics