This is what I wrote (with some help from the users at
developersdex.com). It queries a directory, compiles a list that is x
number of days old, deletes the files, emails you a copy of the list of
files, and deletes the file list (using a couple of batch files that
could easily be worked in).
====================
Option Explicit
Dim oFS, sSourceDir, nDays, nTotal, sFileList
Set oFS = WScript.CreateObject("Scripting.FileSystemObject")
'set nDays to be the number of days since last accessed
'(6 months is 180 days)
nDays = 180
'Put your directory you want to query as the sSourceDir
'sSourceDir = "\\server\users\JohnDoe"
Call FindOldFiles(oFS.GetFolder(sSourceDir))
If nTotal > 1 Then
' MsgBox "WARNING: Time to archive files!" & vbCrlf & _
'
"------------------------------------------------------------------" &
vbCrlf & vbCrlf & _
' "You have " & nTotal & " file(s) that need to be archived!"
& vbCrlf & sFileList, vbInformation ,"Archive Check"
'------------------------------
' Write out results to log file
Dim FileSystemObject, TextStream
Set FileSystemObject = CreateObject("Scripting.FileSystemObject")
Set TextStream = FileSystemObject.CreateTextFile("C:\oldfiles.LOG",
True)
TextStream.WriteLine ("This script has deleted " & nTotal & " file(s)
from some folder on some server") & vbCrlf & _
"--------------------------------------------------" & vbCrlf & _
sFileList ', vbInformation, "Archive Check"
TextStream.Close
'------------------------------
' Email results
Dim stdout, shell
Dim cmd
function ExecCmd(cmdline)
set stdout = wscript.stdout
set shell = createobject("wscript.shell")
set cmd = shell.exec(cmdline)
do until cmd.status = 1 : wscript.sleep 5 : loop
ExecCmd = cmd.stdout.readall
End Function
ExecCmd ("oldfile.bat")
'----------------------
'
' oldfile.bat
'
' blat c:\oldprofiles.log -s "Old Profiles that have
' not been accessed in 180 days on SomeServer"
' -t (e-mail address removed)
' del c:\oldprofiles.log
'
' -------------------------
Else
MsgBox "No files need to be archive", vbInformation ,"Archive Check"
End If
'
WSCript.Quit
'---------------------
Sub FindOldFiles(oFolder)
Dim oFile, oSubFolder
For Each oFile In oFolder.Files
If IsOlder(oFile, nDays) Then
nTotal = nTotal + 1
sFileList = sFileList & oFile.Path & vbCrlf
'--------
'
' Deletes old files
'
'--------
oFS.GetFile (oFile)
oFS.DeleteFile (oFile)
'--------
'
' Comment out the lines above if do not want to delete
'
'--------
End If
Next
For Each oSubFolder In oFolder.SubFolders
Call FindOldFiles(oSubFolder)
Next
End Sub
'---------------------
Function IsOlder(oFile, nDays)
IsOlder = False
If DateDiff("d", oFile.DateLastAccessed, Now) > nDays Then IsOlder =
True
End Function