Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > VB Script

Vista - delete files older than x days from a folder and its subfolders

Reply
 
Old 02-25-2009   #1 (permalink)
Chris


 
 

delete files older than x days from a folder and its subfolders

looking for a script or something will let me schedule it. Only plan to do
it once a year so might have lots of log files. Each log is less than 2 MB.

Thanks.

My System SpecsSystem Spec
Old 02-25-2009   #2 (permalink)
Pegasus \(MVP\)


 
 

Re: delete files older than x days from a folder and its subfolders


"Chris" <Chris@xxxxxx> wrote in message
news:5EEB2BC9-A84B-41C4-B646-24ED83D7B8A3@xxxxxx
Quote:

> looking for a script or something will let me schedule it. Only plan to
> do
> it once a year so might have lots of log files. Each log is less than 2
> MB.
>
> Thanks.
Here you go. Make sure to run the script with active=false until you're
satisfied that it deletes the right files!

Const Active = False
Const sSource = "d:\My Files"
Const MaxAge = 10 'days
Const Recursive = True

Checked = 0
Deleted = 0

Set oFSO = CreateObject("Scripting.FileSystemObject")
if active then verb = "Deleting """ Else verb = "Old file: """
CheckFolder oFSO.GetFolder(sSource)

WScript.echo
if Active then verb = " file(s) deleted" Else verb = " file(s) would be
deleted"
WScript.Echo Checked & " file(s) checked, " & Deleted & verb

Sub CheckFolder (oFldr)
For Each oFile In oFldr.Files
Checked = Checked + 1
If DateDiff("D", oFile.DateLastModified, Now()) > MaxAge Then
Deleted = Deleted + 1
WScript.Echo verb & oFile.Path & """"
If Active Then oFile.Delete
End If
Next

if not Recursive then Exit Sub
For Each oSubfolder In oFldr.Subfolders
CheckFolder(oSubfolder)
Next
End Sub


My System SpecsSystem Spec
Old 02-26-2009   #3 (permalink)
Chris


 
 

Re: delete files older than x days from a folder and its subfolder

The script is GREAT!! Just what I need. Thanks!!

"Pegasus (MVP)" wrote:
Quote:

>
> "Chris" <Chris@xxxxxx> wrote in message
> news:5EEB2BC9-A84B-41C4-B646-24ED83D7B8A3@xxxxxx
Quote:

> > looking for a script or something will let me schedule it. Only plan to
> > do
> > it once a year so might have lots of log files. Each log is less than 2
> > MB.
> >
> > Thanks.
>
> Here you go. Make sure to run the script with active=false until you're
> satisfied that it deletes the right files!
>
> Const Active = False
> Const sSource = "d:\My Files"
> Const MaxAge = 10 'days
> Const Recursive = True
>
> Checked = 0
> Deleted = 0
>
> Set oFSO = CreateObject("Scripting.FileSystemObject")
> if active then verb = "Deleting """ Else verb = "Old file: """
> CheckFolder oFSO.GetFolder(sSource)
>
> WScript.echo
> if Active then verb = " file(s) deleted" Else verb = " file(s) would be deleted"
> WScript.Echo Checked & " file(s) checked, " & Deleted & verb
>
> Sub CheckFolder (oFldr)
> For Each oFile In oFldr.Files
> Checked = Checked + 1
> If DateDiff("D", oFile.DateLastModified, Now()) > MaxAge Then
> Deleted = Deleted + 1
> WScript.Echo verb & oFile.Path & """"
> If Active Then oFile.Delete
> End If
> Next
>
> if not Recursive then Exit Sub
> For Each oSubfolder In oFldr.Subfolders
> CheckFolder(oSubfolder)
> Next
> End Sub
>
>
>
My System SpecsSystem Spec
Old 02-28-2009   #4 (permalink)
Pegasus \(MVP\)


 
 

Re: delete files older than x days from a folder and its subfolder


"Chris" <Chris@xxxxxx> wrote in message
news:FE9E9E65-99F6-4CD8-BF05-EA207BE38D50@xxxxxx
Quote:

> The script is GREAT!! Just what I need. Thanks!!
>
> "Pegasus (MVP)" wrote:
Thanks for the feedback.


My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
copy text files only from all subfolders within a folderto a another folder VB Script
List files in folder with subfolders at top? Vista file management
Delete Files Older than 6 Months PowerShell
Delete files which are not modified in 30 days PowerShell
Deleting all files older than one hour in a folder PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46