Deleting old files 
Author Message
 Deleting old files

Hi,
I have made this script that makes backups from files like my documents. So
i like to make my script delete files older than 15 days from the zip disk
when it copies the new backup file in there. Is it possible to do this with
VBS? If it is how it is done? Thanks!


Sun, 16 May 2004 03:08:20 GMT  
 Deleting old files

Quote:
> Hi,
> I have made this script that makes backups from files like my documents.
So
> i like to make my script delete files older than 15 days from the zip disk
> when it copies the new backup file in there. Is it possible to do this
with
> VBS? If it is how it is done? Thanks!

Here is a demo script I wrote that deletes files older than a spcified
number of days from the specified directory and all its subdirectories.
Notice that the script has been been defanged. Instead of actually deleting
the files, it pops the "would have been deleted" file's name and how many
days old it is. To give the script back its fangs, remove the coment from
Line 023 and comment out Line 024.

Of course you need to change Line 007 to point to your particulat
destination directory and change Line 008 to the number of days old you want
to keep files.

[--- Begin: Delete Old Logs.Vbs ---]

001.   Option Explicit
002.
003.   Dim oFS, sDestDir, nDays, nTotal
004.
005.   Set oFS = WScript.CreateObject("Scripting.FileSystemObject")
006.
007.   sDestDir = "C:\Logs"
008.   nDays = 7
009.
010.   Call DelOldFiles(oFS.GetFolder(sDestDir))
011.
012.   MsgBox nTotal & " files deleted!", ,"All Done!"
013.   WSCript.Quit
014.
015. '---------------------
016.   Sub DelOldFiles(oFolder)
017.     Dim oFile, oSubFolder
018.
019.     For Each oFile In oFolder.Files
020.       On Error Resume Next
021.       If IsOlder(oFile, nDays) Then
022.         nTotal = nTotal + 1
023.         'oFile.Delete
024.         MsgBox "File: " & oFile.Path & vbCrlf & "Days Old: " &
DateDiff("d", oFile.DateLastModified, Now), , "Would have been deleted!"
025.       End If
026.     Next
027.     ' Process any sub folders
028.     For Each oSubFolder In oFolder.SubFolders
029.       Call DelOldFiles(oSubFolder)
030.     Next
031.   End Sub
032.
033. '---------------------
034.   Function IsOlder(oFile, nDays)
035.     IsOlder = False
036.     If DateDiff("d", oFile.DateLastModified, Now) > nDays Then IsOlder
= True
037.   End Function

[--- End: Delete Old Logs.Vbs ---]



Sun, 16 May 2004 07:48:14 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. can't delete old files

2. How do I delete old files?

3. VBScript to replace DOS Batch that deletes old files

4. VBScript to Replace Dos Batch....Deleting Old Files

5. need code to delete old files

6. Delete oldest file

7. Script to delete files older than a specific date in a directory

8. Delete Files 3 days old

9. Find files in a folder and delete them if older then 2 dayx

10. Delete files older than n days.

11. VBS to delete files in a directory older than 30 days

12. Delete files when they are X days old.

 

 
Powered by phpBB® Forum Software