Solved

VBScript:  File Count of Folder and Subfolders

Posted on 2006-10-27
3
7,966 Views
Last Modified: 2012-06-21
Greetings, Experts.  I'm looking for VBScript code that will allow me to accomplish the following goals:

1)  perform a count of the total number of files within a server folder, including the total number of files in any subfolders that may exist.
2)  send an e-mail to select users, persuming that the server has Outlook installed.  For example:  "The total number of files for <path> is ###."

Is this possible?  Does this exist?

Thanks, Experts!

Todd
0
Comment
Question by:todjklki
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 35

Accepted Solution

by:
mvidas earned 125 total points
ID: 17821455
Hi Todd,

There isn't an existing function to do exactly this, but you can always write it :)

Change the foldername, smtpserver, and destination addresses as needed.  If you don't have CDO installed on the machine, this could be changed to use the outlook object.

 Dim FSO, vPath, FileCnt
 Set FSO = CreateObject("scripting.filesystemobject")
 vPath = "C:\folder name\"
 FileCnt = 0
 ReturnFileCountUsingFSO vPath, FileCnt
 If FileCnt > 0 Then
  SendMailCDO "The total number of files in '" & vPath & "' is " & FileCnt, "mysmtpserver"
 End If
 Set FSO = Nothing
 Set vPath = Nothing
 Set FileCnt = Nothing

Function ReturnFileCountUsingFSO(vPath, FileCnt)
 Dim f, fld
 On Error Resume Next 'in case no permission for folder
 Set fld = FSO.GetFolder(vPath)
 FileCnt = FileCnt + fld.Files.Count
 For Each f In fld.SubFolders
  ReturnFileCountUsingFSO f.path, FileCnt
 Next
 On Error GoTo 0
 Set f = Nothing
 Set fld = Nothing
End Function

Function SendMailCDO(strBody, SmtpServer)
 Dim objCDO
 Set objCDO = Nothing
 On Error Resume Next
 Set objCDO = CreateObject("CDO.Message")
 On Error GoTo 0
 If objCDO Is Nothing Then 'cdo must not be installed on the machine
  SendMailCDO = False
  Exit Function
 End If
 With objCDO
  .Subject = "File Count"
  .From = "FileCount@domain.com" 'address does not have to exist
  .To = "joe.smith@domain.com;john.johnson@domain.com"
  .TextBody = strBody
  With .Configuration.Fields
   .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
   .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = SmtpServer
   .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
   .Update
  End With
  .Send
 End With
 Set objCDO = Nothing
 SendMailCDO = True
End Function

Matt
0
 

Author Comment

by:todjklki
ID: 17821495
TOO SWEET!  Awesome work, Matt!

Todd
0
 
LVL 35

Expert Comment

by:mvidas
ID: 17821529
Glad to help!
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Six Sigma Control Plans

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question