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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

delete files in DOS

Status
Not open for further replies.

ashley75

Technical User
Oct 4, 2002
94
US
Hi all,

I'm needing to set up a simple batch script to run every day to delete old backup files which are older than 7 days.

thanks,
 
ashley75,
not dos but vbs:
Code:
Option Explicit
on error resume Next

Dim obj_FSO
Dim char_Dir
Dim obj_Folder
Dim obj_FileColl
Dim obj_File
Dim obj_ArcFile1
Dim int_DaysOld
Dim char_date

int_DaysOld = 7 'age in days
char_Dir = "C:\yourdir\here" 'target dir
char_date = Year(Now) & Right("0" & Month(Now),2) & Right("0" & Day(Now),2)
Set obj_FSO = CreateObject("Scripting.FileSystemObject")
Set obj_Folder = obj_FSO.GetFolder(char_Dir)
Set obj_FileColl = obj_Folder.Files
Set obj_ArcFile1 = obj_FSO.CreateTextFile("c:\yourdir\logdir\DelFile1_" & char_date & ".txt", True)

'DateLastModified is what is shown via explorer in windose
'can also use DateCreated and DateLastAccessed based upon needs

For each obj_File in obj_FileColl
	If DateDiff("d", obj_File.DateLastModified,Now ()) > int_DaysOld Then
		obj_ArcFile1.WriteLine(obj_File & "    " & obj_File.DateLastModified & "    Deleted")
		obj_File.Delete(True)
	End If
Next
obj_ArcFile1.Close

'clear objects
Set obj_FSO = Nothing
Set obj_Folder = Nothing
Set obj_FileColl = Nothing
Set obj_File = Nothing
Set obj_ArcFile1 = Nothing
should do the job, but make sure to test.
regards,
longhair
 
Here's another example.

Code:
'First, you will need to setup a scheduled task and run it
'daily.  Just change the iDaysOld number to the amount of days you
'want to keep file in that directory.  Any files in the
'directory will be deleted.  This file must be place WITHIN
'the directory you wish to clean.
'Courtesty of monsterjta @ tek-tips.com

'Start

Option Explicit
on error resume next
    Dim oFSO
    Dim sDirectoryPath
    Dim oFolder
    Dim oFileCollection
    Dim oFile
    Dim iDaysOld
    Dim CurDir

'Definitions
    iDaysOld = 5
    Set oFSO = CreateObject("Scripting.FileSystemObject")
    sDirectoryPath = CreateObject("WScript.Shell").CurrentDirectory
    sDirectoryPath = sDirectory & "\"
    set oFolder = oFSO.GetFolder(sDirectoryPath)
    set oFileCollection = oFolder.Files 

'Walk through each file in this folder collection. 
    For each oFile in oFileCollection
        If oFile.DateLastModified < (Date() - iDaysOld) Then
            oFile.Delete(True)
        End If
    Next

'Clean up
    Set oFSO = Nothing
    Set oFolder = Nothing
    Set oFileCollection = Nothing
    Set oFile = Nothing
'End

Hope This Helps,

Good Luck!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top