Reputation: 1702
the following VB script , will remove the files under Temp dir and contain the word access.log
how to change this VB script in order to remove only files that contain the word "access.log" and are old then 1 or 2 or 3 ... month
I want to add in the VB some parameter that will contain the month Number and the files will be deleted according to this parameter
for example if Month_do_del=12
Then only files that contain access.log that old then 12 month will be deleted
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
Upvotes: 0
Views: 491
Reputation: 528
Change this line
If Instr(UCase(objFile.Name), UCase ("access.log")) Then
to this
If Instr(1, UCase(objFile.Name), UCase("access.log")) And DateDiff("m", objFile.DateLastModified, Date) >= 12 Then
Upvotes: 1
Reputation: 2859
I don't know if FileDateTime
, Date
and DateDiff
are supported in VBS. If not you can easily port your VBS code to VBA. DateDiff will find the difference between two dates in months.
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
Upvotes: 1