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"
}
}
}
01
thru07
... 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]Set-ItemProperty -Path $cpath -Name CreationTime -Value $_.CreationTime