Powershell – modify Date and Time of a file to reflect its filename

Question:

I have had to recover a load of files from a memory card to my hdd. The names for the recovered files on my hdd are in the format “yyyymmdd_hhmmss.mp4”, and do show the correct times & dates.

However, the date modified column for these recovered files shows quite a different date (in the format “dd/mm/yyyy hh:mm” (obviously the seconds show when viewing their properties) as I reside in the UK), and I would like these date modified stamps to reflect their filenames once again. I have started to write a PowerShell script to extract the date & time to variables, and this is what I’ve done so far:

However, my experience with PS cannot write the date modified attribute to reflect the filename. Please could someone help me?

Kind regards,

Rob Hughes.

Answer:

To set the “date modified” property, update the $file.LastWriteTime property. You can use [datetime]::ParseExact() to parse the date and time from the files name in a single operation.

Source:

Powershell – modify Date and Time of a file to reflect its filename by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply