How to Delete Files + Log what is happening with VB Script

mcclunyboy

Registered User.
Local time
Yesterday, 23:15
Joined
Sep 8, 2009
Messages
292
Hi,

I have the following script which is successful. Can someone help me add logging so I can monitor which files are getting deleted.

I have text file which I would like to use for the logging. My existing script is simply (works well).

Code:
Option Explicit
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
DeleteFiles fso.GetFolder("E:\Backups\ProgressBackups") '<-----UNC Path to folder

'---------------------------------------------------------------------------
' --- Deletes files which haven't been modified within the last 4 days ----
'---------------------------------------------------------------------------

Sub DeleteFiles(srcFolder)
    Dim srcFile
    For Each srcFile in srcFolder.Files
        If DateDiff("d", Now, srcFile.DateLastModified) < -4 Then
    fso.DeleteFile srcFile, True
    End If
    Next
End Sub
I know I need another FSO and the use of writeline, but can someone give me some help?
 
Hi,

I have the following script which is successful. Can someone help me add logging so I can monitor which files are getting deleted.

I have text file which I would like to use for the logging. My existing script is simply (works well).

Code:
Option Explicit
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
DeleteFiles fso.GetFolder("E:\Backups\ProgressBackups") '<-----UNC Path to folder

'---------------------------------------------------------------------------
' --- Deletes files which haven't been modified within the last 4 days ----
'---------------------------------------------------------------------------

Sub DeleteFiles(srcFolder)
    Dim srcFile
    For Each srcFile in srcFolder.Files
        If DateDiff("d", Now, srcFile.DateLastModified) < -4 Then
    fso.DeleteFile srcFile, True
    End If
    Next
End Sub
I know I need another FSO and the use of writeline, but can someone give me some help?


It shouldn't be too hard:

Code:
Option Explicit
Dim fso
[COLOR=Red]Dim Logger[/COLOR]
Set fso = CreateObject("Scripting.FileSystemObject")
[COLOR=Red]Set Logger = Scripting.FileSystemObject.OpenTextFile("C:\path\to\your\file.txt", ForWriting, True)[/COLOR]
DeleteFiles fso.GetFolder("E:\Backups\ProgressBackups") '<-----UNC Path to folder

'---------------------------------------------------------------------------
' --- Deletes files which haven't been modified within the last 4 days ----
'---------------------------------------------------------------------------

Sub DeleteFiles(srcFolder)
    Dim srcFile
    For Each srcFile in srcFolder.Files
        If DateDiff("d", Now, srcFile.DateLastModified) < -4 Then
[COLOR=Red]    Logger.WriteLike(srcFile)[/COLOR]
    fso.DeleteFile srcFile, True
    End If
    Next
[COLOR=Red]Logger.Close[/COLOR]
End Sub

Hope this help
 
Thanks Ripper - with your help I got it working - it is a useful piece of code so here is the outcome:

Code:
Option Explicit On

[COLOR=Red]Const ForWriting = 8[/COLOR]
Dim fso, [COLOR=Red]fso2
Dim Logger[/COLOR]
Dim DateStamp

Set fso = CreateObject("Scripting.FileSystemObject")
[COLOR=Red]Set fso2 = CreateObject("Scripting.FileSystemObject")
Set Logger = fso2.OpenTextFile("C:\Backups\BackupTask\log.txt", ForWriting, True)[/COLOR]

DateStamp = Date()

DeleteFiles fso.GetFolder("E:\Backups\ProgressBackups")


Sub DeleteFiles(ByVal srcFolder)
    Dim srcFile
   [COLOR=Red] Logger.writeline(" ")[/COLOR]
    For Each srcFile In srcFolder.Files
         If DateDiff("d", Now, srcFile.DateLastModified) < -4 Then
            [COLOR=Red]Logger.WriteLine ("File " & srcFile &" Was Deleted at " & Date)
            fso.DeleteFile srcFile, True[/COLOR]
        End If
    Next
    [COLOR=Red]Logger.writeline(" ")
    Logger.Close()[/COLOR]
End Sub
 
Last edited:

Users who are viewing this thread

Back
Top Bottom