Home > Uncategorized > Scripting: a file deletion script (older than X days)

Scripting: a file deletion script (older than X days)

' a good portion lifted and re-written from http://www.tek-tips.com/viewthread.cfm?qid=947098&page=1

set Args = wscript.arguments
set fso=createobject("scripting.filesystemobject")
subfoldersplz = 0

redim preserve arrAllTheFileObjects(0)' declare and definte the array size as 0

EmailBody = "This is the list of files and folders deleted from the S: share: " & vbnewline


function Main()
 for i = 0 to args.count - 1
  if args(i) = "-s" then
   subfoldersplz = 1
  elseif fso.folderexists(args(i)) then
   call EnumerateFiles(args(i), arrAllTheFileObjects)
   ifyougetcaught("wrong line")
  end if

end function

function EnumerateFiles(strRootDirectoryPath, arrAllTheFileObjects)

 set objFolder = fso.getfolder(strRootDirectoryPath)

 intFileCount = objFolder.files.count
 if subfoldersplz = 0 then
  wscript.echo "this script isn't operating recursively."
  wscript.stdout.write "Is this what you intended? [y or n] "
  input = wscript.stdin.readline
  if not input = "y" then
   ifyougetcaught("no recursive")
  end if
 end if
 if intFileCount > 0 then
  i = 0

  arrSize = ubound(arrAllTheFileObjects) 'size of the array
  redim preserve arrAllTheFileObjects(arrSize + intFileCount) 'adjust the size of the array, while maintaining it's members
  for each fileobject in objFolder.Files 'collection
   'add members to array that are the .path properties of the File objects specified above
   'note that this populates the array with all file objects... see: http://msdn.microsoft.com/en-us/library/1ft05taf(VS.85).aspx
   i = i + 1
   arrAllTheFileObjects(arrSize + i) = fileobject 'array only stores path as string, not the fileobject
 end if

 if subfoldersplz = 1 then
  intSubFolderCount = objFolder.subfolders.count
  if intSubFolderCount > 0 then
   for each subfolderobject in objFolder.SubFolders
    call EnumerateFiles(subfolderobject.path,arrAllTheFileObjects)

    'inspect the contents of the objfile.parentfolder and, if there are no files, delete this folder
    set filesinParentFolder = subfolderobject.files
    if filesinParentFolder.count = 0 then 'if folder object doesn't have any fileobjects in it then delete the folder
     EmailBody = EmailBody & subfolderobject.path & vbnewline
    end if
  end if
 end if

end function

function FileAction(arrAllTheFileObjects)

'again, the array didn't store the file objects, just the .path string of the file objects.  Why? no one knows
 for i = 1 to ubound (arrAllTheFileObjects) - 1

  if len(arrAllTheFileObjects(i)) > 0 then
   set objFile = fso.GetFile(arrAllTheFileObjects(i)) 'create a fso file object given the file path
   if DateDiff("D", objFile.DateLastModified, Now) > 7 Then
    EmailBody = EmailBody & objFile.path & vbnewline
   end if
  end if

end function

function EmailOutcome(arrAllTheFileObjects)
 if len(EmailBody) > 67 then 'then there were files and folders appended
  wscript.echo EmailBody
  Set WshShell = CreateObject("WScript.Shell")
  Set oExec = WshShell.Exec("cscript \\server\scripts$\sendmail.vbs /from:FS-gremlin@externaldomain.com /to:alerts@externaldomain.com /subject:""File deletion report for Share"" /body:""" & EmailBody & """")
 end if
end function

function ifyougetcaught(errorstring)

 if errorstring = "wrong line" then
  wscript.echo ""
  wscript.echo "You're feeding me wrong arguments, fool."
  wscript.echo "csript file_deleter.vbs [-s] ""c:\this path"" "
 elseif errorstring = "no recursive" then
  wscript.echo ""
  wscript.echo "If you want recursive, then use -s as the first argument:"
  wscript.echo "csript file_deleter.vbs [-s] ""c:\this path"" "
  wscript.echo "some unknown error occured"
 end if
 wscript.echo "script halted"
end function
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: