VB 脚本 + 删除包含 word 和早于 X 个月的文件

VB script + delete files that contain word and older then X month

以下 VB 脚本,将删除 Temp 目录下的文件并包含单词 access.log

如何更改此 VB 脚本以仅删除包含单词 "access.log" 且旧的文件然后是 1 或 2 或 3 ... 月

我想在 VB 中添加一些包含月份数字的参数 文件将根据此参数删除

例如如果Month_do_del=12

那么只有那些包含 access.log 的文件将被删除,然后是 12 个月

   If LCase(Right(Wscript.FullName, 11)) = "wscript.exe" Then
     strPath = Wscript.ScriptFullName
     strCommand = "%comspec% /k cscript  """ & strPath & """"
     Set objShell = CreateObject("Wscript.Shell")
     objShell.Run(strCommand), 1, True
     Wscript.Quit
 End If

Set objNetwork = CreateObject("WScript.Network")
strLog = "Files deleted on " & objNetwork.ComputerName & " at " & Now &    VbCrLf & "===================================================="
  Set objFSO = CreateObject("Scripting.FileSystemObject")
strFilesToDelete = ""
ShowSubFolders objFSO.GetFolder("G:\Temp")
 If InStr(strFilesToDelete, "|") > 0 Then
arrFiles = Split(strFilesToDelete, "|")
For Each strFilePath In arrFiles
    DeleteFile strFilePath
Next
  ElseIf strFilesToDelete <> "" Then
DeleteFile strFilesToDelete
  Else
WScript.Echo "No files were found."
strLog = strLog & VbCrLf & "No files were found."
 End If

 Set objLogFile = objFSO.CreateTextFile("C:\FileDeletionLog.log", True)
 objLogFile.Write strLog
 objLogFile.Close
 Set objLogFile = Nothing

  Sub ShowSubFolders(Folder)
On Error Resume Next
For Each objFile In Folder.Files
    If Err.Number <> 0 Then
        WScript.Echo "Error reading " & Folder.Path
           strLog = strLog & VbCrLf & "Error reading " &    Folder.Path
        Err.Clear
        On Error Resume Next
        Exit For
     Else
        On Error GoTo 0
             If Instr(UCase(objFile.Name), UCase  ("access.log"))     Then
            If strFilesToDelete = "" Then
                strFilesToDelete = objFile.Path
            Else
                        strFilesToDelete =    strFilesToDelete & "|" & objFile.Path
            End If
        End If
    End If
   Next
    For Each Subfolder in Folder.SubFolders
    ShowSubFolders Subfolder
  Next
  End Sub




  Sub DeleteFile(strFilePath)
On Error Resume Next
WScript.Echo "Deleting " & strFilePath
objFSO.DeleteFile strFilePath, True
If Err.Number <> 0 Then
    WScript.Echo "Could not delete " & strFilePath
    strLog = strLog & VbCrLf & "Could not delete " & strFilePath
    Err.Clear
    On Error GoTo 0
Else
    On Error GoTo 0
        strLog = strLog & VbCrLf & "Successfully deleted " & strFilePath
 End If
End Sub

不知道VBS是否支持FileDateTimeDateDateDiff。如果没有,您可以轻松地将 VBS 代码移植到 VBA。 DateDiff 将计算两个日期之间的月差。

Function CheckMonths(nMonths As Integer, fPath As String) As Boolean
    CheckMonths = False
    If DateDiff("m", FileDateTime(fPath), Date) = nMonths Then
        CheckMonths = True
    End If
End Function

更改此行

If Instr(UCase(objFile.Name), UCase  ("access.log"))     Then

至此

If Instr(1, UCase(objFile.Name), UCase("access.log")) And DateDiff("m", objFile.DateLastModified, Date) >= 12 Then