Solved

VBScript:  File Count of Folder and Subfolders

Posted on 2006-10-27
3
7,933 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
  • 2
3 Comments
 
LVL 35

Accepted Solution

by:
mvidas earned 125 total points
Comment Utility
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
Comment Utility
TOO SWEET!  Awesome work, Matt!

Todd
0
 
LVL 35

Expert Comment

by:mvidas
Comment Utility
Glad to help!
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
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 …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now