?
Solved

VBScript:  File Count of Folder and Subfolders

Posted on 2006-10-27
3
Medium Priority
?
7,975 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 500 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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
This article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Six Sigma Control Plans

765 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