Mxkert
Mxkert

Reputation: 346

Rename files by reformatting the date string embedded in their names

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

Upvotes: 2

Views: 87

Answers (4)

user6811411
user6811411

Reputation:

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

Upvotes: 1

mklement0
mklement0

Reputation: 437953

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

Upvotes: 2

Michael B.
Michael B.

Reputation: 574

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

Upvotes: 0

Lieven Keersmaekers
Lieven Keersmaekers

Reputation: 58441

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

Upvotes: 0

Related Questions