Domanda

I have a bunch of files that all have a name format like:

'This is an example filename - 1st September 2013'
'This is an example filename - 23rd October 2013'

Using PowerShell I'd like to rename the files like so:

'File-20130901'
'File-20131023'

The part I'm having trouble with is converting the date string to a recognised date format.

With a date string that was a recognised format I would do something like:

PS> $date = '23 October 2013'
PS> Get-Date $date -Format 'yyyyMMdd'

But I'm having trouble with these date strings because of the ordinal month-day suffixes.

How would you approach this?

È stato utile?

Soluzione

You could use a regex to remove the ordinal. Here is an example:

(Get-Date ('23rd October 2013' -replace '(\d+)(\w+)(.+)', '$1$3')).ToString('\File-yyyyMMdd')
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top