sirbounty
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?
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'))" }
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
But I'd check your script's logic to make sure it was checking a new file object.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I think Mr. Schutt's answer has it right. I had never heard of this behavior before.
ASKER
How can a 'new' file be generated (even though I'm probably using a -append param) and have a created date of yesterday?