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

How to automatically delete files

Status
Not open for further replies.

bitbrain

Programmer
Jun 10, 1999
159
US
I'm looking for a way of automatically deleting files older than 30 days. I figured I could use the @ scheduler but am not sure how to set-up the delete script.<br>
<br>
Any ideas? Any suggestions will be appreciated.<br>

 
Hi!<br>
<br>
You can probably find a utility somewhere that lets you delete files based upon the last accessed date. Or you could get a NT port of the UNIX ls and xargs commands and write a simple batch file to do it.<br>
<br>
Tim<br>

 
I ended-up writting a VB program to delete the files. If anyone is interested, the source code follows:<br>
<br>
<br>
Private Sub Main()<br>
<br>
' This program will delete old files from the current directory.<br>
' It accepts an argument that determines how old the files should be before they are<br>
' deleted.<br>
' To use the program, first create a text file of the current directory be entering:<br>
' dir &gt; dir.txt<br>
' This program reads the dir.txt file and parses out the file names.<br>
' The file date from the dir command is used to calcualte the age of the file.<br>
' Files older than the 'day' argument will be deleted.<br>
<br>
Dim i As Integer, intRecCnt As Integer, intDays As Integer<br>
Dim intStartPosition As Integer, intEndOfRecordPosition As Integer, intArgDays As Integer<br>
Dim strDOSFilename As String, strFileName As String<br>
Dim strFileList(2000) As String, strTempFile As String<br>
Dim dFileDate As Date, dCurrDate As Date<br>
Dim strArgs As String, strMsg As String<br>
<br>
' get the command line argument(s)...<br>
strArgs = Command<br>
<br>
' strFileName is used to determine if the file exists<br>
strDOSFilename = "dir.txt"<br>
strFileName = Dir(strDOSFilename)<br>
<br>
' perform edits...<br>
If strArgs = "" _<br>
Or strFileName = "" Then<br>
strMsg = "This progam deletes files from the current directory that are" & vbCrLf & _<br>
"greater than n days old." & vbCrLf & vbCrLf & _<br>
"To use it, create a text file of file names by entering the following DOS command:" & vbCrLf & vbCrLf & _<br>
"dir &gt; dir.txt" & vbCrLf & vbCrLf & _<br>
"Then, execute this program passing the number of days. For example," & vbCrLf & _<br>
"to delete files older than 45 days, enter:" & vbCrLf & vbCrLf & _<br>
"DeleteOldFiles 45"<br>
MsgBox strMsg<br>
Exit Sub<br>
End If<br>
<br>
If IsNumeric(strArgs) Then<br>
intArgDays = strArgs<br>
Else<br>
MsgBox "Invalid 'number of days' argument passed" & vbCrLf & vbCrLf & strArgs<br>
Exit Sub<br>
End If<br>
<br>
<br>
' now, continue processing...<br>
<br>
On Error Resume Next<br>
Open strDOSFilename For Input Access Read As #1<br>
If Err Then<br>
MsgBox "Can't open file: " & strDOSFilename<br>
Exit Sub<br>
End If<br>
<br>
'Read the whole file into a single string<br>
strTempFile = Input$(LOF(1), #1)<br>
Close #1<br>
<br>
' Parse the file-string into individual records and put results into an array<br>
' by finding the CRLF characters<br>
<br>
intRecCnt = 0<br>
intStartPosition = 1<br>
intEndOfRecordPosition = InStr(intStartPosition, strTempFile, Chr$(13)) 'CR = 13<br>
While intEndOfRecordPosition &gt;= intStartPosition<br>
intRecCnt = intRecCnt + 1<br>
<br>
strFileList(intRecCnt) = _<br>
Mid$(strTempFile, intStartPosition, intEndOfRecordPosition - intStartPosition) + Chr$(0)<br>
<br>
intStartPosition = intEndOfRecordPosition + 2<br>
intEndOfRecordPosition = InStr(intStartPosition, strTempFile, Chr$(13))<br>
Wend<br>
<br>
dCurrDate = Date<br>
<br>
' identify the records that have file names, exclude directories, check the days, & delete<br>
For i = 1 To intRecCnt<br>
<br>
' records with file names have a date on the left, identify the date by looking for the slash;<br>
' exclude directories<br>
If Mid(strFileList(i), 3, 1) = "/" _<br>
And Not Mid(strFileList(i), 26, 3) = "DIR" Then<br>
<br>
' convert the text date into a date variable<br>
dFileDate = Format(Left(strFileList(i), 8), "MM/DD/YY")<br>
<br>
' calculate the age of the file<br>
intDays = DateDiff("d", dFileDate, dCurrDate)<br>
<br>
' if the number of days exceedes the passed number of days, delete the file<br>
If intDays &gt; intArgDays Then<br>
<br>
' delete the file<br>
Kill (Right(strFileList(i), Len(strFileList(i)) - 39)) ' file name is at the end of the record<br>
<br>
End If<br>
End If<br>
Next i<br>
End Sub<br>
<br>

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top