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:
1 2 3 4 5 6 7 8 9 10 |
foreach ($file in Get-ChildItem *.mp4) { $yy = $file.Name.substring(0,4) $mm = $file.Name.substring(4,2) $dd = $file.Name.substring(6,2) $hh = $file.Name.substring(9,2) $min = $file.Name.substring(11,2) $ss = $file.Name.substring(13,2) } |
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.
1 2 3 4 5 |
foreach($file in Get-ChildItem -Filter *.mp4) { $file.LastWriteTime = [datetime]::ParseExact($file.BaseName, 'yyyyMMdd_HHmmss', $null) } |