2

I am using Windows' Powershell in the CMD to remove 'Mon, tue, wed' etc. from my filenames, which works perfectly

Get-ChildItem -recurse -name | ForEach-Object { Move-Item $_ $_.replace("Mon ", "") }
Get-ChildItem -recurse -name | ForEach-Object { Move-Item $_ $_.replace("Tue ", "") }
Get-ChildItem -recurse -name | ForEach-Object { Move-Item $_ $_.replace("Wed ", "") }
Get-ChildItem -recurse -name | ForEach-Object { Move-Item $_ $_.replace("Thur ", "") }
Get-ChildItem -recurse -name | ForEach-Object { Move-Item $_ $_.replace("Fri ", "") }
Get-ChildItem -recurse -name | ForEach-Object { Move-Item $_ $_.replace("Sat ", "") }

Now my file names look like this: 13 July 2018 - Lorem ipsum

I would like to switch the day with the month, so it will be: July 13 2018, so I could sort it by month. Or maybe even 2018 July 13.

How could I do this?

Thanks, Mike

mklement0
  • 382,024
  • 64
  • 607
  • 775
Mxkert
  • 346
  • 2
  • 5
  • 17
  • 3
    Why not parse the date and re-output it in YYYY-MM-DD format? Months as names will not be (usefully) sortable. – Adrian Wragg Mar 15 '19 at 12:14
  • Well they are downloaded files from the interwebz, which have the date of the blogarticle in the filename. So I would like to keep it simple and just switch the months and year etc. So I can sort it easily in my folder. – Mxkert Mar 15 '19 at 12:26

4 Answers4

2

You can combine both desired transformations into a single operation using Rename-Item with a delay-bind script block in which the -replace operator allows you to transform the file name as needed based on a regex (regular expression).

Get-ChildItem -Recurse -Name | Rename-Item -NewName {
  $_.Name -replace '\w+ (\d+) (\w+) (\d+)', '$3 $2 $1'
} -WhatIf

-WhatIf previews the renaming operation; remove it to perform actual renaming.

An input file named Mon 13 July 2018 - Lorem ipsum, for instance, would be renamed to
2018 July 13 - Lorem ipsum.

Note: This sample file name happens to have no filename extension, but both the solutions above and below equally work with file names that do have extensions.

For more information about PowerShell's -replace operator, see this answer.


If you wanted make your filenames truly sortable using an embedded format such as 2018-07-13 to represent 13 July 2018, more work is needed, via the -split operator:

Get-ChildItem -Recurse -Name | Rename-Item -NewName {
  # Split the name into the date part (ignoring the weekday) and the
  # rest of the file name.
  $null, $date, $rest = $_.Name -split '\w+ (\d+ \w+ \d+)'
  # Convert the date string to [datetime] instance, reformat it, and
  # append the rest.
  ([datetime] $date).ToString('yyyy-MM-dd') + $rest
} -WhatIf

An input file named Mon 13 July 2018 - Lorem ipsum, for instance, would be renamed to
2018-07-13 - Lorem ipsum.

For more information about PowerShell's -split operator, see this answer.
Assigning to multiple variables ($null, $date, $rest = ...) is explained in help topic about_Assignment_Operators

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

Not an answer to your question, IMO mklement0's answer serves best.

But a replacement for your ugly suboptimal sample code.

The RegEx based -replace operator is superior to the .replace() method
when having alternations to replace.

[Globalization.DatetimeFormatInfo]::CurrentInfo.AbbreviatedDayNames

returns the abbreviated Day Names for the current locale, which can be combined
in one RegEx "(Sun|Mon|Tue|Wed|Thu|Fri|Sat) " with the code

$RE=[regex]"("+([Globalization.DatetimeFormatInfo]::CurrentInfo.AbbreviatedDayNames -join '|')+") "

Get-ChildItem -recurse -File | Rename-Item -NewName {$_.Name -replace $RE} -WhatIf

An empty replacement string doesn't need to expressed with the -replace operator.

If the output looks OK, remove the trailing -WhatIf

0

You could chain replacements for each month and end with a replace statement to switch the numbers like this

"13 July 2018 - Lorem ipsum" `
    -replace 'July', '07' `
    -replace 'Aug', '08' `
    -replace "(\d+) (\d+) (\d+)", '$3 $2 $1'

wich returns

2018 07 13 - Lorem ipsum
Lieven Keersmaekers
  • 57,207
  • 13
  • 112
  • 146
0

You can use following code to convert the date

$string = '13 July 2018 - Lorem ipsum'
$dateObject = [datetime]$string.Split('-')[0]
Write-Output "$($dateObject.ToString('yyyy MMMM dd')) - $($array[1])"

This will output

2018 July 13 -  Lorem ipsum
Michael B.
  • 558
  • 3
  • 11