Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

deleting files older than * days

6 views
Skip to first unread message

MonkeytownGuru

unread,
Jun 11, 2008, 12:40:01 PM6/11/08
to
Can a script be created to delete files older than "x" amount of days, not
just past a particular date but calculate the age of a file and delete them
once they reach, perhaps, 31 days? If so, any guidance would be appreciated.

Reventlov

unread,
Jun 11, 2008, 5:21:04 PM6/11/08
to
Il giorno Wed, 11 Jun 2008 09:40:01 -0700, =?Utf-8?B?TW9ua2V5dG93bkd1cnU=?=
<Monkeyt...@discussions.microsoft.com> ha scritto:

>Can a script be created to delete files older than "x" amount of days, not
>just past a particular date but calculate the age of a file and delete them
>once they reach, perhaps, 31 days? If so, any guidance would be appreciated.

Dim fso, f, f1, fc
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder("J:\aaaa")
Set fc = f.Files
For Each f1 in fc
If DateDiff("d", f1.DateLastModified, Now) > 31 Then
f1.Delete
End If
Next
Set fso = Nothing
Set f = Nothing
Set fc = Nothing


--
Giovanni Cenati (Bergamo, Italy)
Write to "Reventlov" at katamail com
http://digilander.libero.it/Cenati (Esempi e programmi in VbScript)
--

MonkeytownGuru

unread,
Jun 18, 2008, 10:36:00 AM6/18/08
to
Reventlov: So simple but so effective - Much appreciated

Reventlov

unread,
Jun 18, 2008, 6:00:54 PM6/18/08
to
Il giorno Wed, 18 Jun 2008 07:36:00 -0700, =?Utf-8?B?TW9ua2V5dG93bkd1cnU=?=
<Monkeyt...@discussions.microsoft.com> ha scritto:

>Reventlov: So simple but so effective - Much appreciated

to tell you the truth, the code was taken from an old post in another newsgroup. ;-)
Giovanni.

chidical

unread,
Oct 28, 2009, 3:43:06 PM10/28/09
to
Excellent script. A good example of 'to-the-point' code that gets the job
done. Thanks for sharing.

Reventlov

unread,
Oct 28, 2009, 5:37:39 PM10/28/09
to
Il giorno Wed, 28 Oct 2009 12:43:06 -0700, =?Utf-8?B?Y2hpZGljYWw=?=
<chid...@discussions.microsoft.com> ha scritto:

>Excellent script. A good example of 'to-the-point' code that gets the job
>done. Thanks for sharing.

I have tons of these scripts.
All copied from other newsgroups and web sites. ;-)

amar

unread,
Feb 1, 2010, 9:36:01 AM2/1/10
to
But how to delete perticualr files like .doc or .ldf

mediaXpert

unread,
Feb 7, 2010, 10:01:01 AM2/7/10
to
Simply add this around the DateDiff statement:

fileExt = fso.GetExtensionName(f1.Path)
If fileExt = "doc" Or fileExt = "ldf" Then


If DateDiff("d", f1.DateLastModified, Now) > 31 Then
f1.Delete
End If

End If

Dowley

unread,
Mar 22, 2010, 6:24:01 AM3/22/10
to
Is there a way this can scan subfolders as well?

Reventlov

unread,
Mar 22, 2010, 5:28:07 PM3/22/10
to
Il giorno Mon, 22 Mar 2010 03:24:01 -0700, =?Utf-8?B?RG93bGV5?=
<Dow...@discussions.microsoft.com> ha scritto:

>Is there a way this can scan subfolders as well?
>
>"Reventlov" wrote:
>> Dim fso, f, f1, fc
>> Set fso = CreateObject("Scripting.FileSystemObject")
>> Set f = fso.GetFolder("J:\aaaa")
>> Set fc = f.Files
>> For Each f1 in fc
>> If DateDiff("d", f1.DateLastModified, Now) > 31 Then
>> f1.Delete
>> End If
>> Next
>> Set fso = Nothing
>> Set f = Nothing
>> Set fc = Nothing

I never tried.
You could try to put this into a sub and if the directory entry is a subdir, then call the
sub recoursively.
Maybe it's easier to shell a DIR /S/B >LIST.TXT
and check every entry of the txt file.

Ram

unread,
Mar 23, 2010, 8:11:01 AM3/23/10
to
Hi

How i can delete a files older than a 30 days with filename*.extension
(f*.txt)....

Can anyone help

0 new messages