Script that iterates through a directory tree to locate all files that have
' a 'last accessed' date older than a certain date (or age in days). It then
' logs the files and their full path to an output file of your choosing.
'
' DIRECTIONS:
' Use with command line arguments.
' /path: followed by the full directory path to the target directory
' /age: followed by the age of the files in days
' /date: followed by the cutoff date in the dd/mm/yyyy format
' /output: followed by the path and name of the output file (i.e., c:\output.txt)
' if /output: is not specified it will default to c:\output.txt
Option Explicit
'On Error Resume Next
Dim strTargetDir, strErrorFile, strOutputFile, intCutoffAge, intOldLogExists, objFSO, objFile, strNotificationMessage, colNamedArguments, _
strTargetDate, intFileCount, intFileSizeCount, intFileSize
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
Const OverwriteExisting = True
intFileCount = 0
intFileSizeCount = 0
intFileSize = 0
Set colNamedArguments = WScript.Arguments.Named
If colNamedArguments.Exists("output") Then
strOutputFile = colNamedArguments.Item("output")
Else
strOutputFile = "c:\output.txt"
End If
If colNamedArguments.Exists("path") Then
strTargetDir = colNamedArguments.Item("path")
Else
Wscript.Echo "Please specify a target directory via the /path: command line argument."
Wscript.Quit 1
End If
If colNamedArguments.Exists("date") and colNamedArguments.Exists("age") Then
Wscript.Echo "You cannot use the /date: and /age: command line arguments at the same time. Please use one or the other, but not both."
Wscript.Quit 1
Else
End If
If colNamedArguments.Exists("date") Then
strTargetDate = CDate(colNamedArguments.Item("date"))
intCutoffAge = DateDiff("d", strTargetDate, Now)
Else
End If
If colNamedArguments.Exists("age") Then
intCutoffAge = CInt(colNamedArguments.Item("age"))
Else
End If
If intCutoffAge = "" and strTargetDate = "" Then
Wscript.Echo "Please specify either a cutoff date or age in days using the /date: or /age: command line argument."
Wscript.Quit 1
Else
End If
Set objFSO = CreateObject("Scripting.FileSystemObject")
' Checks for old output file and deletes it, then creates a new one
If objFSO.FileExists(strOutputFile) Then
objFSO.DeleteFile(strOutputFile)
Else
End If
Set objFile = objFSO.CreateTextFile(strOutputFile, OverwriteExisting)
objFile.Close
Wscript.Echo "Directory Tree Crawler started at " & Now
Wscript.Echo " Hit CTRL-C to exit."
Wscript.Echo "Target: " & strTargetDir
Wscript.Echo "Date: " & strTargetDate
Wscript.Echo "Age: " & intCutoffAge
CrawlTree strTargetDir
intFileSizeCount = FormatNumber(intFileSizeCount / 1024, 2)
Wscript.Echo intFilecount & " files at " & intFileSizeCount & " kilobytes meet the selected criteria."
Set objFile = objFSO.OpenTextFile(strOutputFile, ForAppending)
objFile.Writeline intFilecount & " files at " & intFileSizeCount & " kilobytes meet the selected criteria."
objFile.Close
Sub CrawlTree(strTargetDir)
Dim objFolder, arrFolders, objFiles, Item, Item2
Set objFolder=objFSO.GetFolder(strTargetDir)
Set arrFolders=objFolder.SubFolders
Set objFiles=objFolder.Files
' Get all sub-folders in this folder
For Each Item In arrFolders
If Right(Item, 3) = "-NP" Then
Wscript.Echo Now & " -- " & item & " ends with -NP. Skipping directory tree."
ElseIf Right(Item, 10) = "ANOTHER" Then
Wscript.Echo Now & " -- " & item & " ends with ANOTHER. Skipping directory tree."
Else
If Len(Item) < 256 Then
CrawlTree(item)
Else
Wscript.Echo Now & " -- " & item & " is deeper than 256 characters (" & Len(Item) & " characters). Skipping directory tree."
End If
End If
Next
Item2=0
'Scan through the files collection, find files older than the target age and adds to output.
For Each Item2 in objFiles
Dim strAccessDate, strCreatedate, objFileName, intDaysOld
Set objFileName = objFSO.GetFile(Item2)
strAccessDate = objFileName.DateLastAccessed
intDaysOld = DateDiff("d", strAccessDate, Now)
If intDaysOld > intCutoffAge Then
Wscript.Echo Now & " -- " & objFileName.Path & " is " & intDaysOld & " days old." & " Depth=" & Len(objFileName.Path)
If Len(objFileName.Path) < 256 Then
Set objFile = objFSO.OpenTextFile(strOutputFile, ForAppending)
objFile.Writeline objFileName.Path
objFile.Close
intFileCount = intFileCount + 1
intFileSize = objFileName.Size
intFileSizeCount = intFileSizeCount + intFileSize
Else
Wscript.Echo Now & " -- " & item & " is deeper than 256 characters (" & Len(Item) & " characters). Skipping directory tree."
End If
Else
End If
Next
End Sub