Need a Batch, VB or Powershell script that can parse a file name for a date, and then delete that file older than 90 days.

I am looking for a batch, VB or Powershell script that can parse a file name for a date, and then delete that file if the Date is older than 90 days. Files will be in subfolders, so the script would need to be able to drill down into the subfolders.
Here example of file names:

2019-01-02T080219~queue~520118152~+520118152~311242d8-10ea-49ae-869c-81a43434d40b.WAV
2018-05-10T112639~call~1004~605191811~c97043a6-2bfe-44c8-bde4-b0c3596ede60.WAV
2018-05-02T144725~call~1005~151981173112~a272d339-d85e-4742-a6d0-a988ba6594ea.WAV
2018-10-23T144526~call~2411~18189199~6774130d-a249-4c18-8700-8c629bdf3841.WAV
LVL 2
Glenn MSystems Engineer / ManagerAsked:
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.

Jose Gabriel Ortega CastroEE Rookie/Solution Guide/Topic Advisor and CEO Faru Bonon ITCommented:
This is a solution on powershell with the use of a class, there you go:

#requires -version 5
#number of days old
[int]$global:days=90

class file{
    [Datetime]$date
    [string]$QueueType
    [string]$phone1
    [string]$phone2
    [string]$fileName
    
    file(){}
    
    file([string]$split){
        $splited = $split.split('~')
        $this.date = [Datetime]::ParseExact($splited[0],"yyyy-MM-ddTHHmmss",$null)
        $this.QueueType = $splited[1]
        $this.phone1 = $splited[2]
        $this.phone2 = $splited[3]
        $this.fileName = $splited[4]
    }
}

$ClassArray= @()


get-content .\file.txt | %{
    
    $newclass = [file]::new($_)  #New-Object file -ArgumentList $a
    $comparativeDate = (Get-Date).AddDays(-$global:days)

    if($newclass.date -gt $comparativeDate){
        $ClassArray +=$newclass
    }
}

#result
$ClassArray 

Open in new window

David Johnson, CD, MVPRetiredCommented:
#>
$path = "c:\temp"
$date = get-date
$maxdays = 200
$tokeepdate = $date.AddDays(-1 * $maxdays).ToString("yyyy-MM-dd")
$files = Get-ChildItem -Path $path -Recurse
foreach($file in $files)
    {
    if ($tokeepdate -gt  $file.Name.Substring(0,10) )
        {
        Remove-Item $file.Name -WhatIf   
        }
    }
 

Open in new window

oBdACommented:
This PowerShell script will only process .wav files that start with a date in the given format.
It's in test mode and will only display what it would delete. Remove the -WhatIf at the end of the last line to run it for real.
$deleteDate = (Get-Date).AddDays(-90)
Get-ChildItem -Path 'C:\Test' -Filter *.wav -Recurse -File |
	Where-Object {($_.BaseName -match '(?<DT>\d{4}-\d{2}-\d{2}T\d{6})') -and ([DateTime]::ParseExact($Matches['DT'], 'yyyy-MM-ddTHHmmss', $null) -lt $deleteDate)} |
	Remove-Item -WhatIf

Open in new window

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
PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

Bill PrewIT / Software Engineering ConsultantCommented:
You've got some Powershell options, so here are some other thoughts.

This would be cumbersome in a pure BAT script, it doesn't handle date calculations very well.  But if you want to consider that I can dig one up.

It could be done with a combination of BAT and a small VBS routine for the date checking, let me know if you are interested in that.

If you wanted to deleted based on the files creation date rather than it's name, then you could just use:

forfiles /p "c:\temp" /m "????-??-??T*.wav" /d -30 /c "cmd /c del @file"

Where "c:\temp" is the folder where the files reside.

If you wanted to use the creation date instead of the modification date then you could grab this very handy free utility and get the job done that way, also with a single command line.



»bp
Glenn MSystems Engineer / ManagerAuthor Commented:
testing each one!  brb
Glenn MSystems Engineer / ManagerAuthor Commented:
David, is that a VB script or something else?
Bill PrewIT / Software Engineering ConsultantCommented:
David, is that a VB script or something else?
Looks like Powershell.


»bp
Glenn MSystems Engineer / ManagerAuthor Commented:
David, I get an error on yours.

Exception calling "Substring" with "2" argument(s): "Index and length must refer to a location within the string.
Parameter name: length"
At S:\David.ps1:9 char:9
+     if ($tokeepdate -gt  $file.Name.Substring(0,10) )
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ArgumentOutOfRangeException
#>
$path = "S:\David"
$date = get-date
$maxdays = 200
$tokeepdate = $date.AddDays(-1 * $maxdays).ToString("yyyy-MM-dd")
$files = Get-ChildItem -Path $path -Recurse
foreach($file in $files)
    {
    if ($tokeepdate -gt  $file.Name.Substring(0,10) )
        {
        Remove-Item $file.Name -WhatIf   
        }
    }

Open in new window

Glenn MSystems Engineer / ManagerAuthor Commented:
Thank you. Your solution was spot on!
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.