Link to home
Start Free TrialLog in
Avatar of sirbounty
sirbountyFlag for United States of America

asked on

powershell and file dates

I'm writing to a log file and wanting a backup made if the creation date is earlier than today.
But apparently, even after 'renaming' the file, once it's recreated (when next written to), it retains the same creation date (yesterday) causing this block to be hit again (and obviously failing since the file already exists).

What can I do to ensure the 'new' file is written with today's date stamp?
$today = (get-date).Date
if (test-path $logFile) {
    $filedate = (Get-ChildItem $logFile).CreationTime.Date
    if ( $today -gt $filedate ) { 
        Rename-Item $logFile "$($filedate.tostring('yyyyMMdd'))_$($($MyInvocation.MyCommand.Name).Replace('.ps1','.txt'))" } 
}

Open in new window

SOLUTION
Avatar of David Johnson, CD
David Johnson, CD
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sirbounty

ASKER

Reasoning?  
How can a 'new' file be generated (even though I'm probably using a -append param) and have a created date of yesterday?
Rename does not change creation date. Nor does appending.

But I'd check your script's logic to make sure it was checking a new file object.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I think Mr. Schutt's answer has it right. I had never heard of this behavior before.