PowerShell

Set Creation- and Modification-Date from Filename

After my return of my last vacation, I had a bunch of videos that I wanted to edit, transcode and archive. First I renamed the files based on their creation date so that this information is being preserved and consistent during the whole process.
When my editing and transcoding was done I just needed to set the file’s modification and creation date back to when it was originally taken. To do this job I created a small PowerShell Script I want to share with you:

$Directory = "C:PathtoFiles"
$DateFormat = "yyyy-MM-dd_HHmmss"


foreach ($file in (Get-ChildItem $Directory)) {
    $date_from_file=[datetime]::ParseExact($file.Name.Substring(0,$file.Name.Length-4), $DateFormat, $null)

    $file.CreationTime = $date_from_file
    $file.LastAccessTime = $date_from_file
    $file.LastWriteTime = $date_from_file

    Write-Host ($file.Name + " - " + $date_from_file)
}

Here’s the result – the date / timestamp in the filename is identical with the actual file date:

Set Creation- and Modification-Date from Filename

You may also like
Update: Assign Office 365 licenses based on Active Directory groups (version 1.1)
PowerShell Best Practices: Set-StrictMode
Safely change the Administrator Password
1 Comment
  • Mark
    2017-09-02T00:58:16+00:000000001630201709 at 00:58
    Reply

    Thanks for this, saved me a lot of work today šŸ™‚

Leave Your Comment

Your Comment*

Your Name*
Your Webpage