Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBScript to email when a folder has certain # of files.

Status
Not open for further replies.
Oct 23, 2010
2
US
Hello -

I'm in need of some VBScript code.

We have a directory in one of our servers where, if it reaches a certain number of files, certain functionalities will not operate.

What I am seeking is a code that will automatically send an email to a group of users if a certain directory reaches a certain amount of files.

Is there a post someplace that can explain this, or can someone provide ready-made code to fulfill this question?

Let me know, and thanks.
 
[1] There are two main functional parts (a) send out email of notification to a group of users; and (b) monitoring a directory see if the number of files in it is below a certain number, and if it is more, the (a) will be triggered. That is the way you do the design.

[1.a] You can search script for sending email using cdo or whatever: there are more than you can digest.

[1.b] There is more tricky if you don't know what keywords to serach. You can search wmi event, in particular, you can search wmi monitore file creation... ms has some very explicit demo.

[1.b.1] A further note may help you in constructing the event sink: basically you monitor file creation event. Once the event is trigger, the event sink that consume it can count the number of files in the target folder. If it exceeds some limit, the "send email sub" will be called.

[2] We won't do the work for you though unless you show and convince the forum you've done reasonable and due effort on these.
 
I found a couple of examples online.

This is one that will count the files:

dim oFS, oFolder, threshold
set oFS = WScript.CreateObject("Scripting.FileSystemObject")
set oFolder = oFS.GetFolder("your directory here")

ShowFolderDetails oFolder

sub ShowFolderDetails(oF)
dim F
wscript.echo oF.Name & ":#Files=" & oF.Files.Count
for each F in oF.Subfolders
ShowFolderDetails(F)
next
end sub


And this one will just send an email.

' Begin example
SMTPServer = "your smtp server"
Recipient = "Enter recipient here"
From = "enter sender here"
Subject = "Test email"
Message = "This is a two line test message" & vbcrlf & "Did you get it?"


' To add an attachement update the full path and uncomment the line
' There is one line below that must also be uncommented

'attachment = "c:\test.txt"


' Call Sub and pass required data
GenericSendmail SMTPserver, From, Recipient, Subject, Message



' Begin Sub
' ------------------------------------------------------------------
' Generic function to send mail using a remote
' SMTP server. Pass SMTPserver, From address,
' Recipient address, Subject, and Message as arguments
' ------------------------------------------------------------------
Sub GenericSendmail (SMTPserver, From, Recipient, Subject, Message)

set msg = WScript.CreateObject("CDO.Message")
msg.From = From
msg.To = Recipient
msg.Subject = Subject
msg.TextBody = Message

' To add an attachemt uncomment this line
'msg.AddAttachment attachment

msg.Configuration.Fields(" = SMTPServer
msg.Configuration.Fields(" = 2

msg.Configuration.Fields.Update
msg.Send

End Sub


I'm going to need quite a bit of handholding on this one because by no means am I a programmer.

Thanks for your help.
 
the folder / file recursion would not be the most efficient method of what you want to achieve.
have a look at tsuji's suggestion regarding WMI event notification


I Hear, I Forget
I See, I Remember
I Do, I Understand

Ronald McDonald
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top