Use of Wild Cards in PS Script?

Andrew Mellor
Andrew Mellor used Ask the Experts™
on
Dear Experts,

I currently use a reasonably simple PS script to delete any files older than 7 days.

These files originate from a number of different sources but ultimately reside in the same folder where I run my script.

They are prefixed with a client ID, followed by a sequential identifier...

eg:

CompanyA12345.rar
CompanyA12346.rar
CompanyB23456.rar
CompanyB23457.rar
CompanyC34567.rar
ComapnyC34568.rar

I now have a requirement to reduce the retention of some of the files down to 1 day. For example, files from Company A & B should retain files up to 7 days old, but Company C should only retain files up to 1 day old.

The script I currently use is shown below, and I was wondering if the best way to tackle this problem is to use wildcards. Do you agree? And if so, could you help me out with the syntax?

$limit = (Get-Date).AddDays(-7)
$path = "C:\ExtractedFiles"

# Delete files older than the $limit.
Get-ChildItem -Path $path -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $limit } | Remove-Item -Force

# Delete any empty directories left behind after deleting the old files.
Get-ChildItem -Path $path -Recurse -Force | Where-Object { $_.PSIsContainer -and (Get-ChildItem -Path $_.FullName -Recurse -Force | Where-Object { !$_.PSIsContainer }) -eq $null } | Remove-Item -Force -Recurse

Open in new window


I should add that I'm a simpleton when it comes to PS :-)
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Senior Developer
Commented:
Wildcards are only a viable way, if your client ID has a pattern. Otherwise triage the files. E.g.

Clear-Host

$companies = @( "companyA", "companyB" )
$limit1 = (Get-Date).AddDays(-1)
$limit7 = (Get-Date).AddDays(-7)
$folder = "C:\Temp"

Function InCompanies() {
    Param ([string] $filename)
    $result = $False
    ForEach ($company in $companies) {
        If ($filename.StartsWith($company)) {
            $result = $True
        }
    }

    return $result
}

Get-ChildItem -Path $folder -Force |
    Where-Object {
        !$_.PSIsContainer -and $_.CreationTime -lt $limit7
    } |
    Remove-Item -Force

Get-ChildItem -Path $folder -Force |
    Where-Object {
        !$_.PSIsContainer -and $_.CreationTime -lt $limit1 -and (InCompanies $_.BaseName)
    } |
    Remove-Item -Force

Open in new window

Author

Commented:
Many thanks for this.

The files that are exported to my folder, do begin with a company identifier.

So in your script, am I correct in assuming that the script will remove all files older than 7 days, AND THEN, remove any files beginning with CompanyA or CompanyB that are older than 1 day?

Apologies for the questions.

Regards

Andy

Author

Commented:
I have just tested, and this works perfectly for me. Thanks so much.

Regards

Andy

Author

Commented:
Perfect

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial