PSCX: Set-FileTime

The PowerShell Community Extensions have a cmdlet Set-FileTime that can do the same thing we did in a script earlier.  I have rewritten the script to use the cmdlet

001
002
003
004
005
006
007
008
009
010
011
012
013
014
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
        Set-FileTime -Time $($old.CreationTime) -Created -Path $($_.Fullname) -Force -Verbose
    }
}

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

Pretty much the same as before except we save a couple of lines by using the cmdlet.

Technorati Tags: ,,

Leave a Reply