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

LVL 67
sirbountyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David Johnson, CD, MVPOwnerCommented:
try the modified date
0
sirbountyAuthor Commented:
Reasoning?  
How can a 'new' file be generated (even though I'm probably using a -append param) and have a created date of yesterday?
0
jmcgOwnerCommented:
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.
0
Top Threats of Q1 & How to Defend Against Them

WEBINAR: Join WatchGuard CTO and our Threat Research Team on Aug. 2nd to hear the findings from our Q1 Internet Security Report! Learn more about the top threats detected in the first quarter and how you can defend your business against them!

Robert SchuttSoftware EngineerCommented:
How can a 'new' file be generated (...) and have a created date of yesterday?
Check out File System Tunneling, a backward compatibility feature of windows to make sure a 'safe save' (saving to a new file first and then rename to the wanted filename) does not alter certain properties of the original file. More info here.

I encountered this some time ago and was able to solve it by specifically setting the CreationDate of the file (in .NET code) as it was created again (after being moved) but have never tested changing the registry to disable this feature.
0
Robert SchuttSoftware EngineerCommented:
Maybe you could try something like this after the rename (untested):
New-Item $logFile -type file
(Get-ChildItem $logFile).CreationTime = Get-Date

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jmcgOwnerCommented:
I think Mr. Schutt's answer has it right. I had never heard of this behavior before.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.