Glenn M
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~52 0118152~+5 20118152~3 11242d8-10 ea-49ae-86 9c-81a4343 4d40b.WAV
2018-05-10T112639~call~100 4~60519181 1~c97043a6 -2bfe-44c8 -bde4-b0c3 596ede60.W AV
2018-05-02T144725~call~100 5~15198117 3112~a272d 339-d85e-4 742-a6d0-a 988ba6594e a.WAV
2018-10-23T144526~call~241 1~18189199 ~6774130d- a249-4c18- 8700-8c629 bdf3841.WA V
Here example of file names:
2019-01-02T080219~queue~52
2018-05-10T112639~call~100
2018-05-02T144725~call~100
2018-10-23T144526~call~241
#>
$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
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
ASKER
testing each one! brb
ASKER
David, is that a VB script or something else?
David, is that a VB script or something else?Looks like Powershell.
»bp
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 : ArgumentOutOfRangeExceptio n
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 : ArgumentOutOfRangeExceptio
#>
$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
}
}
ASKER
Thank you. Your solution was spot on!
Open in new window