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

Scheduled Task for deleting files?

Status
Not open for further replies.

a96pirate

MIS
Jul 30, 2002
14
0
0
US
Is there a Utility or a Script that can be scheduled to Run and delete files based on Creation or Modification dates?
 
This is the site,
I'm not sure if this is what your looking at, but below is the source, you could customize it for yourself.

I found this on-line:

Option Explicit
On Error Resume Next
' Deletes old files based on age, access date, extension.
' Written by Eric Phelps
'
'Force "cscript"
Main
Wscript.Quit 0

Sub Main()
Dim fil 'As Scripting.File
Dim fils 'As Scripting.Files
Dim fol 'As Scripting.Folder
Dim fols 'As Scripting.Folders
Dim fs 'As Scripting.FileSystemObject
Dim strDirectories() 'As String
Dim lngCounter 'As Long
Dim dblMaxCreateAge 'As Double
Dim dblMaxAccessedAge 'As Double
Dim strFileTypes 'As String
Const READONLY = 1
Const HIDDEN = 2
Const SYSTEM = 4
If MsgBox ("This program will delete files in and below a directory you designate based on file age, extension, and last access. Continue?", vbOkCancel) = vbCancel Then Wscript.Quit
Set fs = CreateObject("Scripting.FileSystemObject")
If Lcase(Right(Wscript.FullName, 11)) = "wscript.exe" Then
If MsgBox ("This program is being run under WSCRIPT. Results will be stored in a log at " & FileNameInThisDir("DeleteOld.log") & ". Run this program under CSCRIPT if you want a real-time display of activity. Otherwise, a message box will pop up to inform you when the program finishes. Continue?", vbOkCancel, "Delete Old Files") = vbCancel Then Wscript.Quit
Else
If MsgBox ("This program is being run under CSCRIPT. Results will be displayed on-screen and will not be logged. Run this program under WSCRIPT if you want a log file. Continue?", vbOkCancel, "Delete Old Files") = vbCancel Then Wscript.Quit
End If
Redim strDirectories(0)
strDirectories(0) = fs.GetAbsolutePathName(InputBox("Enter path to start deleting at:", "Delete Old Files", FileNameInThisDir("")))
If strDirectories(0) = "" Then Wscript.Quit
strFileTypes = InputBox("Enter 3-character extensions for file types you want deleted", "Delete Old Files", "txt, doc, xls, ppt, gif, jpg")
If strFileTypes = "" Then Wscript.Quit
dblMaxCreateAge = CDbl(InputBox("Enter the minimum time since file creation (in days) of files to delete", "Delete Old Files", "180"))
If dblMaxCreateAge < 1 Then Wscript.Quit
dblMaxAccessedAge = CDbl(InputBox(&quot;Enter the minimum time since last file access (in days) of files to delete&quot;, &quot;Delete Old Files&quot;, &quot;60&quot;))
If dblMaxAccessedAge < 1 Then Wscript.Quit
If MsgBox(&quot;This is your last question. File deletion starts if you click yes. Okay to delete all &quot; & strFileTypes & &quot; in and below the &quot; & strDirectories(0) & &quot; directory created over &quot; & dblMaxCreateAge & &quot; days ago and not accessed for the past &quot; & dblMaxAccessedAge & &quot; days?&quot;, vbYesNo, &quot;Delete Old Files&quot;) = vbNo Then Wscript.Quit
lngCounter = 0
Status &quot;*************************************&quot;
Status &quot;*************************************&quot;
Status &quot;Program: &quot; & Wscript.ScriptFullName
Status &quot;Program started: &quot; & Now
Status &quot;Deleting &quot; & strFileTypes & &quot; in and below &quot; & strDirectories(0) & &quot; older than &quot; & dblMaxCreateAge & &quot; days and not accessed in over &quot; & dblMaxAccessedAge & &quot; days&quot;
Do Until lngCounter > Ubound(strDirectories,1)
'Next folder to process
Set fol = fs.GetFolder(strDirectories(lngCounter))
'Get each file in turn
Set fils = fol.Files
If Err.Number <> 0 Then Exit Sub
For Each fil In fils
If Instr(strFileTypes, Lcase(Right(fil.ShortName,3))) <> 0 Then
If (CDbl(Now) - CDbl(fil.DateCreated)) > dblMaxCreateAge Then
If (CDbl(Now) - CDbl(fil.DateLastAccessed)) > dblMaxAccessedAge Then
If ((fil.Attributes And READONLY) = 0) Then
If ((fil.Attributes And SYSTEM) = 0) Then
If ((fil.Attributes And HIDDEN) = 0) Then
If Lcase(fil.Path) <> Lcase(Wscript.ScriptFullName) Then
Status fil.Path
Status &quot; DELETED&quot;
Status &quot; Date Created &quot; & fil.DateCreated
Status &quot; Date Modified &quot; & fil.DateLastModified
Status &quot; Date Accessed &quot; & fil.DateLastAccessed
fil.Delete
End If
End If
End If
End If
End If
End If
End If
Next
'Check for any sub folders and add them to the folder array
Set fols = fol.SubFolders
For each fol in fols
If Lcase(fol.Name) <> &quot;recycled&quot; Then
Redim Preserve strDirectories(Ubound(strDirectories,1) + 1)
strDirectories(Ubound(strDirectories,1)) = fol.Path
End If
Next
lngCounter = lngCounter + 1
Loop
Status &quot;Program finished: &quot; & Now
Status &quot;*************************************&quot;
Status &quot;*************************************&quot;
If Lcase(Right(Wscript.FullName, 11)) = &quot;wscript.exe&quot; Then MsgBox &quot;Program Finished! Details are in the log at &quot; & FileNameInThisDir(&quot;DeleteOld.log&quot;), vbOkOnly, &quot;Delete Old Files&quot;
End Sub

Function FileNameInThisDir(strFileName) 'As String
'Returns the complete path and file name to a file in
'the script directory. For example, &quot;trans.log&quot; might
'return &quot;C:\Program Files\Scripts\Database\trans.log&quot;
'if the script was in the &quot;C:\Program Files\Scripts\Database&quot;
'directory.
Dim fs 'As Scripting.FileSystemObject
Set fs = CreateObject(&quot;Scripting.FileSystemObject&quot;)
FileNameInThisDir = fs.GetAbsolutePathName(fs.BuildPath(Wscript.ScriptFullName, &quot;..\&quot; & strFileName))
''''''''''Clean up
Set fs = Nothing
End Function

Sub Status (strMessage)
'If the program was run with CSCRIPT, this writes a
'line into the DOS box. If run with WSCRIPT, it writes
'to a log named &quot;DeleteOld.log&quot; in the same directory as
'the script.
Dim ts 'As Scripting.TextStream
Dim fs 'As Scripting.FileSystemObject
Const ForAppending = 8 'Scripting.IOMode
If Lcase(Right(Wscript.FullName, 12)) = &quot;\cscript.exe&quot; Then
Wscript.Echo strMessage
Else
Set fs = CreateObject(&quot;Scripting.FileSystemObject&quot;)
Set ts = fs.OpenTextFile(FileNameInThisDir(&quot;DeleteOld.log&quot;), ForAppending, True)
ts.WriteLine strMessage
ts.Close
''''''''''Clean up
Set ts = Nothing
Set fs = Nothing
End If
End Sub

Sub Force(sScriptEng)
'Forces this script to be run under the desired scripting host
'Valid sScriptEng arguments are &quot;wscript&quot; or &quot;cscript&quot;
'If you don't supply a valid name, Force will switch hosts...
If Lcase(Right(Wscript.FullName, 12)) = &quot;\wscript.exe&quot; Then
'Running under WSCRIPT
If Instr(1, Wscript.FullName, sScriptEng, 1) = 0 Then
'Need to switch to CSCRIPT
CreateObject(&quot;Wscript.Shell&quot;).Run &quot;cscript.exe &quot; & Wscript.ScriptFullName
Wscript.Quit
End If
Else
'Running under CSCRIPT
If Instr(1, Wscript.FullName, sScriptEng, 1) = 0 Then
'Need to switch to WSCRIPT
CreateObject(&quot;Wscript.Shell&quot;).Run &quot;wscript.exe &quot; & Wscript.ScriptFullName
Wscript.Quit
End If
End If
End Sub
 
This link might be better:

NoOldFiles.vbs on the same page that I indicated in my comment above.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top