Quantcast
Channel: THWACK: All Content - All Communities
Viewing all articles
Browse latest Browse all 20518

Problem with VB script to return number of files older than X

$
0
0

I have the following script that I found that will check a directory for files that are older than 1 hour and return the number and file names. The script is working great but I need to add logic for it to also check any subfolders or the main directory. Can anybody help me out with the modifications to the code.


'
' This script will parse content of directory and looking for files older than
' specific time interval (modified) and will generate two output messages containing
' list of names of these files (Message) and number of these files (Statistic).
'
' Constants to modify:
' DIR - folder where to look for files
' UNIT - time unit (s - seconds, n - minutes, h - hours, d - days, m - months, q - quarters, yyyy - years)
' IVAL - time interval (in UNITS)
'
' Example (as set up below): files older than 1 hour in C:\test
'

Option Explicit

const DIR = "C:\test\"
const UNIT = "h"
const IVAL = 1

''' do not modify below this line

const SUCCESS = 0
const FAIL = 1

Dim Fso
Dim Directory
Dim Modified
Dim Files
Dim cnt
Dim fnames

Set Fso = CreateObject("Scripting.FileSystemObject")
Set Directory = Fso.GetFolder(DIR)
Set Files = Directory.Files

fnames = ""
cnt = 0

For Each Modified in Files
  If DateDiff(UNIT, Modified.DateLastModified, Now) > IVAL Then
    fnames = fnames & Modified.Name & ", "
    cnt = cnt + 1
  End If
Next

If cnt > 0 Then
  fnames = Left(fnames, Len(fnames) - 2)
Else
  fnames = "No files found"
End If

WScript.Echo "Message: " & fnames
WScript.Echo "Statistic: " & cnt
WScript.Quit(SUCCESS)


Viewing all articles
Browse latest Browse all 20518

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>