Delete old files

5 pts.
Tags:
Development
I am new to visual basic 6.0. I am trying to create a project which will call a function which will take the file spec and number of days as the parameters and will delete files from that directory which are 32 days old. Please help.. Thanks in advance..

Answer Wiki

Thanks. We'll let you know when a new response is added.

Forget about writing this in VB6. Make the move to VB.NET and look at the system.io.file and system.io.directory libraries for all the functions you need. Then search the msdn.microsoft.com site for more.

Discuss This Question: 5  Replies

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
  • Jgsnsbt
    Actually it's not very difficult using any Win32 APIs in VB5/6. Just go to google and search for visual basic filedate or similar and besides that there are a lot of very good VB sites on the internet. I wouldn't consider .NET because of all the overhead you have to take for that simple task. Anyway, I will have a look through my source codes and if I find anything, I post it here.
    30 pointsBadges:
    report
  • Jgsnsbt
    Of course: there's a built-in function in VB called FileDateTime returning the date and time as a variant when a file was created or modified. With this variable you just use the DateDiff function against a given date to determine if the file is outdated. No need for APIs and definitely not for dotNET.
    30 pointsBadges:
    report
  • MooseDrool
    Try using FileSystemObject. Reference "Microsoft Scripting Runtime" in your project. Public Sub DeleteOldFile(sPath As String, iAge As Integer) Dim FSO As New FileSystemObject Dim aFile As file If FSO.FileExists(sPath) Then Set aFile = FSO.GetFile(sPath) If DateDiff("d", aFile.DateCreated, Now) >= iAge Then aFile.Delete True End If End If End Sub
    0 pointsBadges:
    report
  • MooseDrool
    this is the same as the last only you pass in a folder path instead of an individual file path. Again, reference "Microsoft Scripting Runtime" in your project. Public Sub DeleteOldFiles(sPath As String, iAge As Integer) Dim FSO As New FileSystemObject Dim aFolder As Folder Dim aFile As file If FSO.FolderExists(sPath) Then Set aFolder = FSO.GetFolder(sPath) For Each aFile In aFolder.Files If DateDiff("d", aFile.DateCreated, Now) >= iAge Then aFile.Delete True End If Next End If End Sub
    0 pointsBadges:
    report
  • CharlesJC
    gullu1, You could also use the File System Object, part the Microsoft Scripting Runtime, in your function to get the directories, files, and the attributes that you want to compare, as well as deleting the files that match your criteria. Here is a code snipet that will get you started, just remember to add a reference to the Microsoft Scripting Runtime, first: 'Start of code Option Explicit Private Sub cmdDelete_Click() Dim lFSO As FileSystemObject Dim lFile As file Dim lFiles As Files Dim lFolder As Folder On Error GoTo ErrorHandler Set lFSO = New FileSystemObject Set lFolder = lFSO.GetFolder("c:temp") For Each lFile In lFolder.Files MsgBox "File Name: " & lFile.Name & vbNewLine & _ "Created: " & lFile.DateCreated & vbNewLine & _ "Last Modified: " & lFile.DateLastModified If DateDiff("d", CDate(lFile.DateLastModified), Date) >= 32 Then If MsgBox("Do you want to delete this file?", vbQuestion & vbYesNo, _ "Delete File") = vbYes Then lFile.Delete End If End If Next ExitHere: Set lFSO = Nothing Set lFile = Nothing Exit Sub ErrorHandler: MsgBox "Error: " & Err & vbNewLine & Err.Description, vbExclamation, "FSO Testing" Resume ExitHere End Sub 'End of code I hope this helps. Charles PS: Gee, this was fun.
    0 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following