使用VBScript为已删除的文件创建日志

时间:2013-02-18 12:41:15

标签: vbscript

以下是我删除DAT文件的代码。

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

现在我想创建一个日志文件来存储已删除的文件信息(例如文件的名称,运行脚本的时间以及运行此脚本的用户)。谁能帮我这个 ?

先谢谢..

1 个答案:

答案 0 :(得分:1)

如果我应该选择你的风格模式。

' 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