mabuse.de

techniques

Tidy up your logfiles

There is a simple solution: why not let a script do the work for you? Here we go! This is a small VBScript that runs under any version of MS Windows. Just copy the code into your favourite editor and save the file as TidyUp.vbs. Call it with following syntax:

cscript TidyUp.vbs -f <folderpath> -d <numberofdays> [-r]

This will erase all files in the folder <folderpath> that are at least <numberofdays> old. If you're appending the -r parameter, this will be done recursively. Here's an example: cscript TidyUp.vbs -f c:\temp -d 21 -r will erase all files in c:\temp (and subdirectories) that are at least 21 days old. Easy, huh.

Version 1.1 of this tool has some new features: the new parameter -i allows you to specify a list of file extensions to be used as file filter. The parameter -i "log|tmp|bak" will only apply the delete command to files with an .log, .tmp, and .bak extension. Please use the -i parameter exactly as described, that means with quotation marks "" and with a pipe symbol | for separating the extensions. Wildcards are not supported (yet).

Another parameter, -a, does everything you expect but really erase the files. So if you're not sure and want to try it out, you can do this without harm.

Best used as scheduled task. Enjoy.

Beware: you should manually test and be careful about deleting files! I told you! I won't take any responsibility if you're deleting your text files, image folders etc!  

'
' TidyUp
'
' PURPOSE: erase files (of given type(s)) (recursively) in a given folder that are older than the given number of days
' LAST EDITED: 08-07-17
' AUTHOR(S): mabuse.de
' SYNTAX: cscript -f[older] <folder> -d[ays] <days> [-r[ecursive]] [-[f]i[lter] "<type1>|<type2>|...|<typen>]" [-[f]a[ke]]
' VERSION: 1.1
'

dim FolderName ' as Object
dim NumberOfDays ' as integer
dim RecursiveFlag ' as boolean
dim Filterflag ' as boolean
dim FakeAction ' as boolean
dim objFSO ' as Object
dim FileTypes ' as Array

' main
Init
TidyUp

'
' Init - analyze command line
'
sub Init
dim oArgumente ' as Array
dim i ' as integer

RecursiveFlag = false
Filterflag = false
FakeAction = false
set oArgumente = WScript.Arguments

if oArgumente.Count < 2 then
ShowUsage
WScript.Quit
else
do
if UCase(oArgumente(i)) = "-F" or UCase(oArgumente(i)) = "-FOLDER" then
' folder to operate in
i = i 1
FolderName = oArgumente(i)
elseif UCase(oArgumente(i)) = "-D" or UCase(oArgumente(i)) = "-DAYS" then
' number of days
i = i 1
NumberOfDays = CInt(oArgumente(i))
if NumberOfDays <= 0 then
WScript.Echo "ERROR: days must be > 0"
WScript.Quit
end if
elseif UCase(oArgumente(i)) = "-R" or UCase(oArgumente(i)) = "-RECURSIVE" then
' number of days
RecursiveFlag = true
elseif UCase(oArgumente(i)) = "-A" or UCase(oArgumente(i)) = "-FAKE" then
' just pretend deleting, don't do anything that would harm our precious data
FakeAction = true
elseif UCase(oArgumente(i)) = "-I" or UCase(oArgumente(i)) = "-FILTER" then
' set file filter
i = i 1
Filterflag = true
FileTypes = Split(Replace(oArgumente(i), """", ""), "|", -1, 1)
elseif UCase(oArgumente(i)) = "-H" or UCase(oArgumente(i)) = "-HELP" or oArgumente(i) = "-?" then
ShowUsage
WScript.Quit
end if
i = i 1
loop until i >= oArgumente.Count
end if
end sub

'
' TidyUp - main routine
'
sub TidyUp()
dim objFolder ' as Object
dim colFiles ' as Collection

Set objFSO = CreateObject("Scripting.FileSystemObject")

If objFSO.FolderExists(FolderName) Then
Set objFolder = objFSO.GetFolder(FolderName)
Else
Wscript.Echo "ERROR: Folder does not exist."
WScript.Quit
End If

Set colFiles = objFolder.Files

dim filetype ' as string
for each objFile in colFiles
'WScript.Echo objFile.Name & "/" & objFile.DateCreated & "/" & Abs(DateDiff("d", Now, objFile.DateCreated))
if Abs(DateDiff("d", objFile.DateCreated, Now)) >= NumberOfDays then
if Filterflag then
for each filetype in FileTypes
if filetype = Right(objFile.Name, Len(filetype)) then call DeleteFile(objFolder.Path, objFile.Name, objFSO)
next
else
call DeleteFile(objFolder.Path, objFile.Name, objFile)
end if
end if
Next

if RecursiveFlag then TraverseFolders(objFolder)

end sub

'
' TraverseFolders - traverse Folders recursively
'
sub TraverseFolders(Folderobj)
dim objFolder ' as object
dim Subfolder ' as object
dim colFiles ' as Collection
dim objFile ' as object

for each Subfolder in Folderobj.SubFolders
set objFolder = objFSO.GetFolder(Subfolder.Path)
set colFiles = objFolder.Files
for each objFile in colFiles
'WScript.Echo objFile.Name & "/" & objFile.DateCreated & "/" & Abs(DateDiff("d", Now, objFile.DateCreated))
if Abs(DateDiff("d", objFile.DateCreated, Now)) >= NumberOfDays then
if Filterflag then
for each filetype in FileTypes
if filetype = Right(objFile.Name, Len(filetype)) then call DeleteFile(objFolder.Path, objFile.Name, objFile)
next
else
call DeleteFile(objFolder.Path, objFile.Name, objFSO)
end if
end if
next
TraverseFolders(objFolder)
next
end sub

'
' DeleteFile - do the dirty work
'
sub DeleteFile(FolderPath, Filename, FSObject)
Wscript.Echo "Deleting " & FolderPath & "\" & Filename
if not FakeAction then FSObject.DeleteFile(Filename & "\" & Filename)
end sub

'
' ShowUsage - display some help
'
sub ShowUsage()
WScript.Echo "tidyup" & vbCRLF & _
"Delete (recursively) old files (of given type(s)) in a given folder that are at least <n> day(s) old." & vbCRLF & vbCRLF & _
"cscript tidyup.vbs -f[older] <folder> -d[ays] <days> [-r[ecursive]] [-[f]i[lter] <filterstring>]" & vbCRLF & _
" -f <folder>: folder to erase old files from" & vbCRLF & _
" -d <days>: number of days files must exists to get deleted." & vbCRLF & _
" -r (optional): recursive deletion" & vbCRLF & _
" -i <filterlist> (optional): file filter (in format ""<type1>|<type2>|...|<typen>""; no wildcards, in quotation marks" & vbCRLF & _
" -a (optional): fake action: don't delete but show what would be done" & vbCRLF & _
" -? (optional): this help" & vbCRLF & vbCRLF & _
"examples: cscript tidyup.vbs -f c:\temp -d 20" & vbCRLF & _
" cscript tidyup.vbs -f c:\temp -d 20 -r" & vbCRLF & _
" cscript tidyup.vbs -f c:\temp -d 20 -i ""log|bak|tmp"""
end sub