Change File Dates

Interesting question on the forums about changing the date of files.  Poster was converting a bunch of video files between formats and wanted the file in the new format to have the creation date of the original file.

I had some csv files that i could convert to excel files that mimic the problem

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
Get-ChildItem -Path C:\Test\csvtests -Filter "*.xlsx" | 
Select Name, CreationTime, LastWriteTime

Get-ChildItem -Path C:\Test\csvtests -Filter "*.xlsx" | foreach {
    $oldname =  "$_" -replace ".xlsx", ".csv"
    $oldfile = Join-Path -Path c:\test\csvtests -ChildPath $oldname
    if (Test-Path -Path $oldfile) {
        $old = Get-Item -Path $oldfile
        $new = Get-Item $_.Fullname
        $new.CreationTime = $old.CreationTime
    }
}

Get-ChildItem -Path C:\Test\csvtests -Filter "*.xlsx" | 
Select Name, CreationTime, LastWriteTime

Check the date on the new files.

Get the new files and for each file replace the extension with that of the old file (I am assuming the name stays the same!). Test the old file exists and if so set the new file creation date to the old file creation date.

A final test with get-childitem shows the change.

 

PowerShell Community Extensions has a Set-FileTime cmdlet that can do this

 

Technorati Tags: ,

Leave a Reply