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

VB script to delete files help

Status
Not open for further replies.

web4fun

MIS
Oct 2, 2002
127
US
PROBLEM:

I have a folder called 'Backup' which has subfolders that contain large SQL backup files '.BAK' files and unfortunately I haven't been able to get the SQL maintenance plan's cleanup task to successfully delete the large files so I've resorted to attempt to use a VB script to clean up these older .BAK files and although I have found a few sample VB scripts that delete files based on date...I have not been successful in running it to delete the files in the subfolders...it only deletes the files in the current folder.

GOAL:

I need to have the script run automatically every night and delete any .BAK file in each subfolder older than one day from the current date...only want to keep one .BAK file in each subfolder. I know that there must be a way to accomplish this via VB script. Any assistance would be greatly appreciated. I know that I should be able to create a Windows Task to run every night but I'm having problems with the script.

Any assistance would be greatly appreciated.
 
I wonder the FAQ area has nothing about recursive folder stuff ...
 
Hello PHV, there was one that was pretty darn close but from what I can tell, it deletes everything in the subfolders which is not what I'd like to see happen....I want to keep the most current .BAK file....so I'll keep searching...thanks for the tip.
 
Well if you found something that deletes the files in the subfolders...it will be worth checking out this article that shows you had to compare the date on the file.


--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
I found this script in the FAQ section which will do most of what I want except for deleting from the subfolders....any suggestions?

'-------------- ClearOldFiles.vbs --------------------
Option Explicit
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")

DeleteFilesa fso.GetFolder("C:\temp"), "bak"

Sub DeleteFilesa(srcFolder, strExt)

Dim srcFile

If srcFolder.Files.Count = 0 Then
Wscript.Echo "No File to Delete"
Exit Sub
End If

For Each srcFile in srcFolder.Files
If LCase(Right(strFile.Name, Len(strExt))) = strExt Then
If DateDiff("d", Now, srcFile.DateLastModified) < -2 Then
fso.DeleteFile srcFile, True
End If
End If
Next

'Wscript.Echo "Files Deleted successful"

End Sub

Thanks in advance.
 
Simply make you Sub recursive ...

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top