Link to home
Start Free TrialLog in
Avatar of Glenn M
Glenn MFlag for United States of America

asked on

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
Avatar of J0rtIT
J0rtIT
Flag of Venezuela, Bolivarian Republic of image

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

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

ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Bill Prew
Bill Prew

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
Avatar of Glenn M

ASKER

testing each one!  brb
Avatar of Glenn M

ASKER

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


»bp
Avatar of Glenn M

ASKER

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

Avatar of Glenn M

ASKER

Thank you. Your solution was spot on!