biglebowski
Technical User
Following on from my other thread I have found a script on MS that will delete specific files in folders and all subfolders. Is it possible to change this script so that rather deleting based on modified date it will delete based on the files creation date?
Thanks....
StartTime = Date
Sub Main()
On Error Resume Next
If wscript.arguments.Count<>3 Then Help()
Dim Paths
Redim Paths(-1)
Dim Extensions
Redim Extensions(-1)
Set fso = CreateObject("Scripting.FileSystemObject")
For i = 0 to wscript.arguments.count-1
If instr(1,wscript.arguments(i),":") = 0 Then Help()
Parameter = split(wscript.arguments(i),":")(0)
Select Case Parameter
Case "/path"
If instr(replace(Wscript.Arguments(i),"/path:",""),",") Then 'Multiple paths
For Each Path in split(replace(Wscript.Arguments(i),"/path:",""),",")
Redim Preserve Paths(Ubound(Paths)+1)
Paths(Ubound(Paths)) = Path
Next
Else 'Single Path
Redim Preserve Paths(Ubound(Paths)+1)
Paths(Ubound(Paths)) = replace(Wscript.Arguments(i),"/path:","")
End If
'Verify that all paths exist
For Each Path in Paths
If fso.FolderExists(Path)=False Then Help()
Next
If Err.number<>0 Then Help()
Case "/type"
If instr(replace(Wscript.Arguments(i),"/type:",""),",") Then 'Multiple Extensions
For Each Extension in split(replace(Wscript.Arguments(i),"/type:",""),",")
Redim Preserve Extensions(Ubound(Extensions)+1)
Extensions(Ubound(Extensions)) = Extension
Next
Else 'Single Extension
Redim Preserve Extensions(Ubound(Extensions)+1)
Extensions(Ubound(Extensions)) = replace(Wscript.Arguments(i),"/type:","")
End If
If Err.number <> 0 Then Help()
Case "/age"
Age = cint(replace(lcase(Wscript.Arguments(i)),"/age:",""))
If Err.number<>0 Then Help()
Case Else
Help()
End Select
Next
For each Path in Paths
GetErDone=DeleteLogFiles(Path,Extensions,Age)
Next
End Sub
Function DeleteLogFiles(ByVal Folder,ByVal arrExtensions,ByVal Age)
Set fso = CreateObject("Scripting.FileSystemObject")
Set objfolder = fso.GetFolder(Folder)
For Each File in objfolder.Files
For Each Extension in arrExtensions
If lcase(fso.GetExtensionName(File.Path))=lcase(Extension) Then
If abs(datediff("d",File.DateLastModified,StartTime)) > cint(Age) Then
fso.DeleteFile file.path,True
Exit For
End If
End If
Next
Next
For Each Folder in objFolder.SubFolders
DeleteLogFiles Folder.Path,arrExtensions,Age
Next
End Function
Sub Help()
Dim WshShell, BtnCode
Set WshShell = WScript.CreateObject("WScript.Shell")
msg = "DeleteLogs.vbs cannot be run without the correct command line parameters." & vbcrlf & _
"DeleteLogs.vbs deletes log files older than a specified age." & vbcrlf & vbcrlf &_
"DeleteLogs.vbs [/path:[directory]] [/type:[file extension]] [/age:[days as integer]]" & vbcrlf & vbcrlf &_
"/path:" & vbtab & "Specifies a path(s) to delete log files. Paths must be comma delimited and enclosed by quotes." & vbcrlf &_
"/type:" & vbtab & "Specifies the type of file extension(s). Extension must be comma delimited" & vbcrlf &_
"/age:" & vbtab & "Specifies the number of days old when a file should be deleted." & vbcrlf & vbcrlf &_
"Examples:" & vbcrlf &_
"DeleteLogs.vbs /path:" & """" & "C:\Logs" & """" & " /type:log /age:60" & vbcrlf &_
"DeleteLogs.vbs /path:" & """" & "C:\Logs" & """" & "," & """" & "D:\Logs Files" & """" &" /type:log,txt /age:90"
WshShell.Popup msg, , "DeleteLogs.vbs Help", 0 + 32
wscript.quit
End Sub
Main()
When I was born I was so suprised I didn't talk for 18 months
Thanks....
StartTime = Date
Sub Main()
On Error Resume Next
If wscript.arguments.Count<>3 Then Help()
Dim Paths
Redim Paths(-1)
Dim Extensions
Redim Extensions(-1)
Set fso = CreateObject("Scripting.FileSystemObject")
For i = 0 to wscript.arguments.count-1
If instr(1,wscript.arguments(i),":") = 0 Then Help()
Parameter = split(wscript.arguments(i),":")(0)
Select Case Parameter
Case "/path"
If instr(replace(Wscript.Arguments(i),"/path:",""),",") Then 'Multiple paths
For Each Path in split(replace(Wscript.Arguments(i),"/path:",""),",")
Redim Preserve Paths(Ubound(Paths)+1)
Paths(Ubound(Paths)) = Path
Next
Else 'Single Path
Redim Preserve Paths(Ubound(Paths)+1)
Paths(Ubound(Paths)) = replace(Wscript.Arguments(i),"/path:","")
End If
'Verify that all paths exist
For Each Path in Paths
If fso.FolderExists(Path)=False Then Help()
Next
If Err.number<>0 Then Help()
Case "/type"
If instr(replace(Wscript.Arguments(i),"/type:",""),",") Then 'Multiple Extensions
For Each Extension in split(replace(Wscript.Arguments(i),"/type:",""),",")
Redim Preserve Extensions(Ubound(Extensions)+1)
Extensions(Ubound(Extensions)) = Extension
Next
Else 'Single Extension
Redim Preserve Extensions(Ubound(Extensions)+1)
Extensions(Ubound(Extensions)) = replace(Wscript.Arguments(i),"/type:","")
End If
If Err.number <> 0 Then Help()
Case "/age"
Age = cint(replace(lcase(Wscript.Arguments(i)),"/age:",""))
If Err.number<>0 Then Help()
Case Else
Help()
End Select
Next
For each Path in Paths
GetErDone=DeleteLogFiles(Path,Extensions,Age)
Next
End Sub
Function DeleteLogFiles(ByVal Folder,ByVal arrExtensions,ByVal Age)
Set fso = CreateObject("Scripting.FileSystemObject")
Set objfolder = fso.GetFolder(Folder)
For Each File in objfolder.Files
For Each Extension in arrExtensions
If lcase(fso.GetExtensionName(File.Path))=lcase(Extension) Then
If abs(datediff("d",File.DateLastModified,StartTime)) > cint(Age) Then
fso.DeleteFile file.path,True
Exit For
End If
End If
Next
Next
For Each Folder in objFolder.SubFolders
DeleteLogFiles Folder.Path,arrExtensions,Age
Next
End Function
Sub Help()
Dim WshShell, BtnCode
Set WshShell = WScript.CreateObject("WScript.Shell")
msg = "DeleteLogs.vbs cannot be run without the correct command line parameters." & vbcrlf & _
"DeleteLogs.vbs deletes log files older than a specified age." & vbcrlf & vbcrlf &_
"DeleteLogs.vbs [/path:[directory]] [/type:[file extension]] [/age:[days as integer]]" & vbcrlf & vbcrlf &_
"/path:" & vbtab & "Specifies a path(s) to delete log files. Paths must be comma delimited and enclosed by quotes." & vbcrlf &_
"/type:" & vbtab & "Specifies the type of file extension(s). Extension must be comma delimited" & vbcrlf &_
"/age:" & vbtab & "Specifies the number of days old when a file should be deleted." & vbcrlf & vbcrlf &_
"Examples:" & vbcrlf &_
"DeleteLogs.vbs /path:" & """" & "C:\Logs" & """" & " /type:log /age:60" & vbcrlf &_
"DeleteLogs.vbs /path:" & """" & "C:\Logs" & """" & "," & """" & "D:\Logs Files" & """" &" /type:log,txt /age:90"
WshShell.Popup msg, , "DeleteLogs.vbs Help", 0 + 32
wscript.quit
End Sub
Main()
When I was born I was so suprised I didn't talk for 18 months