Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Deleting Files older that a given date 1

Status
Not open for further replies.

vmaruv

Programmer
Apr 8, 2002
102
US
Hi..

I need to delete files older than a particular date in a given directory using VB Script. How can I go about doing this? Your help is really appriciated.

For Eg., I need to delete all the files in "c:\Files" directory which are older than two months (w.r.t.current date). How can I do that ?? I am new to VB Script. I have done this using VB Before but am somehow ubable to do it in VB Script...Pls can you help me ??

Thanks in Advance...

VMaruv
 
You will start with a call like this:

CreateObject("Scripting.FileSystemObject")

This is Microsoft's File System Object (it may also be called "Windows Scripting Host Object Model"). From here you will discover the Folder object which can come from the GetFolder() method of the File System Object. Once you make it here you may have the following:

Set objFiles = objFolder.Files
For Each objFile In objFiles
'Stuff.
Next

Where you do your stuff like call the Delete() method of the File object based on one (or more) of three date properties: DateCreated, DateLastAccessed, DateLastModified.

Bryan Wilhite
rasx@kintespace.com
 
The following should delete all files older tha 60 days in the folder specified :
Code:
Dim objFSO, oFldr, oFile
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set oFldr = objFSO.GetFolder("C:\Files")
For Each oFile In oFldr.Files
    If DateDiff("d", oFile.DateLastModified, Now()) > 60 Then
        oFile.Delete
    End If
Next

A.C.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top