Very new to PowerShell, we have a few older log files and want to delete periodically. So, to achieve this, first trying to filename the log file and getting the date value from the name and after that trying to compare them with the current date, but somehow it is not working? any help would be apricated,
The folder have
01-02-2023-20-31-32.log
02-02-2023-20-30-44.log
29-01-2023-20-30-45.log
30-01-2023-20-30-46.log
31-01-2023-20-29-58.log
Code Block:
function deleteOlderLogFile () {
#get all the file inside the foler
$logFilePath = "C:\Users\LogFolder\";
$currentDate = Get-Date -Format "dd-MM-yyyy";
$olderFileName = @();
$getFileLogFileName = Get-ChildItem -Path $logFilePath;
foreach($fileName in $getFileLogFileName) {
$finalFileName = "$fileName".Substring(0,10);
$olderFileName = $finalFileName ;
}
$finalListOfFolder = $olderFileName -join ","
Write-Host ":"$finalListOfFolder;
##Delete the files
foreach($logFileName in $finalListOfFolder){
if ($logFileName -ge $currentDate) {
Write-Host $logFileName
}
}
}
CodePudding user response:
I had a quick look at your code and figured out some issues. Here is my version of deleteOlderLogFile
.
$logFilePath = "C:\Users\LogFolder\";
$currentDate = Get-Date -Format "dd-MM-yyyy";
$olderFileName = @();
$getFileLogFileName = Get-ChildItem -Path $logFilePath;
foreach($fileName in $getFileLogFileName) {
$finalFileName = "$fileName".Substring(0,10);
$olderFileName = $finalFileName ;
}
$finalListOfFolder = $olderFileName -join ","
Write-Host ":"$finalListOfFolder;
Write-Host "today is" $currentDate
##Delete the files
foreach($logFileName in $olderFileName){
$candidateDate = [Datetime]::ParseExact($logFileName, 'dd-MM-yyyy', $null)
if ($candidateDate -lt $currentDate) {
Write-Host "delete" $logFileName
}else{
Write-Host "do not delete" $logFileName
}
}
Changes:
- Your
foreach
iterates over$finalListOfFolder
which is a string. That's not possible. Iterate over your array$olderFileName
- You did not convert your file names to
DateTime
. Maybe that is unneccessary, but form your question I think that is exactly what you want. Else you will compare aDateTime
to aString
which might not give the results you expect. - You used the
-ge
operator which would delete all files greater or equal to the current date. I think you want to delete old files and therefore you should use-lt
(lesser than). - I added output to make sure which files are not deleted
There is still room for improvement and files are not yet actually deleted, but now it should work well enough.
CodePudding user response:
You should not compare dates as strings, but as DateTime objects (especially since the date format used is non-sortable as it is here).
Assuming the filename of each log file has the date to compare and you want to delete all files that have an older date in their name than today's date, you could do this:
$today = (Get-Date).Date # set to midnight
$logFilePath = 'C:\Users\LogFolder'
Get-ChildItem -Path $logFilePath -Filter '*.log' -File |
Where-Object { $_.BaseName -match '^\d{2}-\d{2}-\d{4}-\d{2}-\d{2}-\d{2}$' } | ForEach-Object {
$fileDate = [datetime]::ParseExact($_.BaseName, 'dd-MM-yyyy-HH-mm-ss', $null)
if ($fileDate -lt $today) {
$_ | Remove-Item -WhatIf
}
}
Please note that I have added a -WhatIf
switch to the Remove-Item cmdlet.
This is a safety measure and with that switch, you will only see a line in the console telling you what action would be performed.
Nothing actually gets deleted.
Once you are satisfied with all console messages that everything does what is expected, you can remove these -WhatIf
switch.
CodePudding user response:
Try
$dates = @("01-02-2023-20-31-32.log","02-02-2023-20-30-44.log","29-01-2023-20-30-45.log","30-01-2023-20-30-46.log","31-01-2023-20-29-58.log")
$table = [System.Collections.ArrayList]::new()
foreach($date in $dates)
{
$newRow = New-Object -TypeName psobject
$newRow | Add-Member -NotePropertyName FileName -NotePropertyValue $date
$dateTime = [DateTime]::parseexact($date.split('\.')[0],"dd-MM-yyyy-HH-mm-ss",$null)
$newRow | Add-Member -NotePropertyName Date -NotePropertyValue $dateTime
$table.Add($newRow) | Out-Null
}
$table = $table | Sort-Object -Property Date -Descending
$table
Results
FileName Date
-------- ----
02-02-2023-20-30-44.log 2/2/2023 8:30:44 PM
01-02-2023-20-31-32.log 2/1/2023 8:31:32 PM
31-01-2023-20-29-58.log 1/31/2023 8:29:58 PM
30-01-2023-20-30-46.log 1/30/2023 8:30:46 PM
29-01-2023-20-30-45.log 1/29/2023 8:30:45 PM