user1619228
user1619228

Reputation: 63

Make log for deleted files using VBScript

The below is my code to delete DAT files.

OPTION EXPLICIT
DIM strExtensionsToDelete,strFolder
DIM objFSO, MaxAge, IncludeSubFolders

' ************************************************************
' Setup
' ************************************************************

' Folder to delete files
strFolder = "E:\test"
' Delete files from sub-folders?
includeSubfolders = true
' A comma separated list of file extensions
' Files with extensions provided in the list below will be deleted
strExtensionsToDelete = "dat"
' Max File Age (in Days).  Files older than this will be deleted.
maxAge = 0

' ************************************************************

set objFSO = createobject("Scripting.FileSystemObject")

DeleteFiles strFolder,strExtensionsToDelete, maxAge, includeSubFolders

wscript.echo "Finished"

sub DeleteFiles(byval strDirectory,byval strExtensionsToDelete,byval maxAge,includeSubFolders)
DIM objFolder, objSubFolder, objFile
DIM strExt

set objFolder = objFSO.GetFolder(strDirectory)
for each objFile in objFolder.Files
    for each strExt in SPLIT(UCASE(strExtensionsToDelete),",")
        if RIGHT(UCASE(objFile.Path),LEN(strExt)+1) = "." & strExt then
            IF objFile.DateLastModified < (Now - MaxAge) THEN
                wscript.echo "Deleting:" & objFile.Path & " | " & objFile.DateLastModified 
                objFile.Delete
                exit for
            END IF
        end if
    next
next    
if includeSubFolders = true then ' Recursive delete
    for each objSubFolder in objFolder.SubFolders
        DeleteFiles objSubFolder.Path,strExtensionsToDelete,maxAge,includeSubFolders
    next
end if
end sub

Now i want to create a log files to store the deleted files information (such as name of the file, when the script is run and user who run this script). Can any one help me with this ?

Thanks in Advance..

Upvotes: 0

Views: 2039

Answers (1)

Panayot Karabakalov
Panayot Karabakalov

Reputation: 3179

If I should go with your style pattern.

' outside your DeleteFiles()
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set LogFile = objFSO.CreateTextFile(objFSO.GetTempName)
LogFile.WriteLine "DateTime: " & Now
LogFile.WriteLine "UserName: " & CreateObject("WScript.NetWork").UserName

DeleteFiles ...

' outside your DeleteFiles()
LogFile.Close

WScript.Echo "Finished"

Sub DeleteFiles(...

    ' inside For..Next in DeleteFiles()
    LogFile.WriteLine objFile.Path

End Sub

Upvotes: 1

Related Questions