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.

Glenn M
Glenn M used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Jose Gabriel Ortega CastroTop Rated Freelancer on MS Technologies
Awarded 2018
Distinguished Expert 2018

Commented:
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

Top Expert 2016

Commented:
#>
$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

Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
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

Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

Bill PrewTest your restores, not your backups...
Top Expert 2016

Commented:
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 / Manager

Author

Commented:
testing each one!  brb
Glenn MSystems Engineer / Manager

Author

Commented:
David, is that a VB script or something else?
Bill PrewTest your restores, not your backups...
Top Expert 2016

Commented:
David, is that a VB script or something else?
Looks like Powershell.


»bp
Glenn MSystems Engineer / Manager

Author

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 / Manager

Author

Commented:
Thank you. Your solution was spot on!

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