Click here to Skip to main content
15,881,938 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The script I am working on needs to delete log files in .jrn format after 90 days. The file name is the date itself. Eg.20200720.jrn but the script deletes all the files after execution. Please help required.
Also for the initials like ABCD20200720.jrn it works perfectly fine but just for the 20200720.jrn it deletes the latest file too.

What I have tried:

$now = Get-Date
Get-ChildItem "C:\Logs" -Filter '*.jrn' | 
foreach {
    if ($_.basename -match " $now (?<year>\d{04})(?<month>\d{2})(?<day>\d{2})$")
    {
        $fileData = Get-Date -Year $Matches.year -Month $Matches.month -Day $Matches.day `
        $_ | Add-Member -MemberType NoteProperty -Name 'DateFromName' -Value $fileData -PassThru
    }
} | Where-Object {($_.DateFromName).addDays(-90) -lt $now} | Remove-Item -Force
Posted
Updated 29-Jul-20 4:25am
v2
Comments
Garth J Lancaster 29-Jul-20 7:35am    
Not sure what you're asking .. are you saying
it SHOULD delete files like ABCD20200720.jrn but
it SHOULD NOT delete files like 20200720.jrn ??

I note nowhere do you check the file for a prefix of 4 characters before the date
AVT007 29-Jul-20 9:53am    
Thanks for your prompt Reply.
I am new to powershell scripts so, I might not be explaining well.
I am saying that the script that I posted works for the files with initials like ABCD20200720.jrn
but some journals are generated purely with date on it like 20200720.jrn with no initials and I need to delete them if they cross 90 days.
So, I want help on my script to delete the 20200920.jrn file.
Right now, the scripts deletes every files in the folder.
Richard MacCutchan 29-Jul-20 10:05am    
Change your script to list the selected items, rather than deleting them, and you will be able to see which ones it is selecting.

1 solution

Your script is looking at the filenames to determine how old they are. You're deleting log files that were CREATED 90 days ago or more, but that's not how old the records in the file are. You could be deleting data that is LESS THAN 90 days old.

Instead of looking at the filenames, why don't you look at the LastModified date of the file? That's going to be the datetime of the last write to the file.
 
Share this answer
 
Comments
AVT007 29-Jul-20 11:26am    
At, first I tried according to the LastModified date. It worked fine. But the problem is logs are fetched remotely from remote system and everyday it is fetched and the DateModified changes to current time and logs are piled. It's not effective for what I am trying to do.
So am trying to delete the file according to filename date not Lastmodified.
Dave Kreskowiak 29-Jul-20 11:38am    
LastModified is not changed unless there is a write to the file or to the properties and attributes of the file.

If the other process that is "fetching" the files is changing the Archive attribute on the file, that will change the LastModified date.
AVT007 29-Jul-20 12:03pm    
Yes, that's the problem.
Since, the logs get fetched remotely into into the server it changes the modification date and I am unable to delete. That's why I am trying to delete the old files as the file name itself are the log generated dates.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900