PowerShell modify LastWriteTime to match file name

Hi,

I was wondering if someone could assist.  I have files from a camera that have their Date Modified times all messed up.  The files are named yyyymmdd_hhminss.jpg.  I would like to write the Date Modified times to match the files names.

I have broken up the file names and applied them to variables, shown here:

foreach ($file in Get-ChildItem *.txt)
    {
    $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, that's as far as I can get - I am having trouble translating that to the $file.LastWriteTime value of the file.

Can someone help?

Rob.

September 7th, 2015 4:41pm

foreach ($file in Get-ChildItem *.txt){
    [datetime]::ParseExact($file.BaseName,'yyyyMMdd_HHmmss',$null)
}
Free Windows Admin Tool Kit Click here and download it now
September 7th, 2015 5:35pm

Thank you for the quick reply, jrv.  Is this supposed to do something apart from display the dates & times of the files onscreen?  I want to modify the actual Date Modified attributes for the files.

September 7th, 2015 6:04pm

Just read my original post.  Apologies, I wasn't too clear on what I wanted to do.
Free Windows Admin Tool Kit Click here and download it now
September 7th, 2015 6:09pm

Have you actually tried to change it?

$file.LastWriteTime=<new time>

September 7th, 2015 6:16pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics