0

Creating a script for work, and they want the logs to roll over for 7 days. For example, todays log is xxxxx.log, and tomorrow that will get renamed to xxxxx.01 and the next day xxxxx.02.

Something weird is happening with my dates however. To test the script is working, I created a file called xxxxx.log and changed the created date (through powershell) to be a few years ago. The script then checks the file to see if the date created was today, if not, then it rolls.

It renamed the file to xxxxx.01 (which is good), and then created a new log file for today, but this new log file has the created date of the old file, and the renamed file has its date changed to a totally different date.

I've tried copy file and then delete, I've tried rename, I've tried move, but they all have the same symptom!

Code below:

Function RollLogs {
#If the file exists, get the creation date of the current log
if($FileDate = (Get-ChildItem -Path "$LogDirectory\$LogFile.$LogExt").CreationTime) {
    #Format the creation date of the file, and compare it to todays date
    #If the date is not equal to today, then roll the logs
    if($FileDate.ToString('yyyyMMdd') -ne $Date.ToString('yyyyMMdd')) {
        #Delete the .07 file, and increment each file by a number
        #Then rename the last file to 0.1
        if(Test-Path "$LogDirectory\$LogFile.07") {Remove-Item -Path "$LogDirectory\$LogFile.07" -Force}
        if(Test-Path "$LogDirectory\$LogFile.06") {Copy-Item -Path "$LogDirectory\$LogFile.06" -Destination "$LogDirectory\$LogFile.07"; Remove-Item -Path "$LogDirectory\$LogFile.06" -Force}
        if(Test-Path "$LogDirectory\$LogFile.05") {Copy-Item -Path "$LogDirectory\$LogFile.05" -Destination "$LogDirectory\$LogFile.06"; Remove-Item -Path "$LogDirectory\$LogFile.05" -Force}
        if(Test-Path "$LogDirectory\$LogFile.04") {Copy-Item -Path "$LogDirectory\$LogFile.04" -Destination "$LogDirectory\$LogFile.05"; Remove-Item -Path "$LogDirectory\$LogFile.04" -Force}
        if(Test-Path "$LogDirectory\$LogFile.03") {Copy-Item -Path "$LogDirectory\$LogFile.03" -Destination "$LogDirectory\$LogFile.04"; Remove-Item -Path "$LogDirectory\$LogFile.03" -Force}
        if(Test-Path "$LogDirectory\$LogFile.02") {Copy-Item -Path "$LogDirectory\$LogFile.02" -Destination "$LogDirectory\$LogFile.03"; Remove-Item -Path "$LogDirectory\$LogFile.02" -Force}
        if(Test-Path "$LogDirectory\$LogFile.01") {Copy-Item -Path "$LogDirectory\$LogFile.01" -Destination "$LogDirectory\$LogFile.02"; Remove-Item -Path "$LogDirectory\$LogFile.01" -Force}
        if(Test-Path "$LogDirectory\$LogFile.$LogExt") {Copy-Item -Path "$LogDirectory\$LogFile.$LogExt" -Destination "$LogDirectory\$LogFile.01"; Remove-Item -Path "$LogDirectory\$LogFile.$LogExt" -Force}
        Log "------------------------------------------------------------------------" "Information"
        Log "Log Roll Complete" "Information"
        Log "------------------------------------------------------------------------" "Information"
    }
}

}

7
  • i don't see any obvious problem ... but i do have a suggestion. instead of 01 thru 07 ... add a datestamp to the file name - something like >>> FileName_20191003.ext <<< then you can sort by that part of the file name and keep only the newest 7. [grin]
    – Lee_Dailey
    Commented Oct 4, 2019 at 0:23
  • Any reason you're copying the logs instead of simply renaming them? Commented Oct 4, 2019 at 0:50
  • I'd just brute force it, make the 01, 02,...07 files an array of objects. Get the create date or whatever from each log before any copy, etc. and set as a variable, and then brute force set that value back after the file rename, copy or whatever operation that's giving you trouble. I'd put it in a loop with the conditional if statements, etc. too perhaps. I wrote about something I whipped up here once too I think for some ideas logic wise.... superuser.com/questions/1366084/… Commented Oct 4, 2019 at 1:00
  • I've tried renaming the files and it still happens. Management wants the 01-07 file structure unfortunately.
    – Freddyg103
    Commented Oct 4, 2019 at 1:06
  • I think he just missed that you already wrote about that. Have you tried setting the value of the creation date or whatever back as per that post from the link I provided? e.g. Set-ItemProperty -Path $cpath -Name CreationTime -Value $_.CreationTime Commented Oct 4, 2019 at 1:12

1 Answer 1

1

This is an old question, but I think I got to the root of the issue, and this should be helpful:

This appears to be a timing issue. The old filename is being reused too quickly, and retaining the creation date of the old version. I worked around it with a start-sleep -seconds 15. 14 seconds or less did not work. I don't know the mechanics of why this is the behavior (some lingering bits that need time to be cleared from memory or file table?).

I saw this same thing, but I was doing it a bit differently. I was creating temp.txt, then renaming production.txt to $date-Production.txt, to keep 15 previous versions. Then renaming temp.txt to production.txt. Then I get *.txt older than 15 days, and delete them. Eventually, all of my renamed files and the production file were all the same date, which became over 15 days old, and everything was deleted!

The date I looked at was the creationdate from get-childitem (same as created: in right-click properties in file explorer).

$date = get-date format yyy-MM-dd
"Production (existing) created on: " #logging
gci Production.txt | Select CreationTime #logging
"create this" | out-file temp.txt
"Temp file created on: " #logging
gci Temp.txt | Select CreationTime #logging
Rename-item production.txt "$date-production.txt"
"Renamed $Date-Production created on: " #logging
gci "$Date-Production.txt" | Select CreationTime #logging
Start-Sleep -Seconds 15 #Adding this fixed it!
Rename-item temp.txt production.txt
"Renamed Production file (was Temp) created on: " #logging 
gci Production.txt | Select CreationTime #logging
#File Cleanup section is below, to delete all .txt older than 15 days
$files2delete = gci *.txt | Where {$_.CreationTime -lt (Get-Date).AddDays(-15)}
$files2delete | Remove-Item -Force

Output (cleaned up a bit) from the above script's logging:

Production (existing) created on: 4/16/2020 9:29:11 AM
Temp file created on:  4/16/2020 9:32:19 AM 
Renamed 2020-04-16_093310-Production created on:  4/16/2020 9:29:11 AM
Sleeping *14* seconds
Renamed Production file (was Temp) created on:  4/16/2020 9:29:11 AM


Production (existing) created on:  4/16/2020 9:29:11 AM
Temp file created on:  4/16/2020 9:33:42 AM
Renamed 2020-04-16_093342-Production created on:  4/16/2020 9:29:11 AM
Sleeping *15* seconds
Renamed Production file (was Temp) created on:  4/16/2020 9:33:42 AM

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .