Delete files by date and character count

I have an interesting problem. I need to delete directories that are older than folder 5 days but are only numerical. We have a log folder that has log files that we need to delete but other files are also stored in here. Normally i would do PS scripts to delete all directories over x amount of days but i can delete all folders. I only need to delete folders that are fully numeric.

Wanted to see if anyone had any ideas?
nopainnogain1Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rob StoneCommented:
Are the folder names the same character length and display?  

Regex is the way to identify it.

Here is an example script which can identify numerical folders (if there is only one block of numbers). You can change the Regex based on what you have in your file system.  

$a = gci C:\Temp |? Mode -match d
$a.Name
$Regex = "\d+"
Foreach ($dir in $a){
    $b=$dir.Name -match $Regex
    if ($b -eq $true){
        $c=$Matches[0] -eq $dir.Name
        if ($c -eq $true){
            $Name = $dir.Name
            Write-Host "$Name identified for deleting"
        }
    }
}

Open in new window

0
footechCommented:
I think Rob Stone is correct about Regex being the way to handle this.  But I think the code can be simplified a fair amount.
Get-ChildItem C:\Temp | Where { $_.PsIsContainer -and $_.Name -match "^\d+$" -and $_.LastWriteTime -gt ((Get-Date).AddDays(-5)) }

Open in new window


Only confusing thing for me is that you go back and forth between talking about files and folders/directories.
0
nopainnogain1Author Commented:
Was able to receive answer on other forum

$root = 'T:\131\root'

Get-ChildItem -Path $root |
Where-Object {
    $_.name -match "^\d*$" -and
    $_.LastWriteTime -lt (Get-Date).AddMinutes(-1)
} |
Remove-Item -Recurse -Force -WhatIf

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
footechCommented:
And you didn't here?
0
nopainnogain1Author Commented:
This gave me a great starting point. It only deleted folders that were numeric
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.