Solved

VB Script that checks for a file extension then send an email is it exists

Posted on 2014-01-29
7
604 Views
Last Modified: 2014-01-29
I am wanting to create a script that goes and checks for a certain file extension and if it exists in a particular folder, then send an email out.  I have this code below, and just to let you know, I'm a novice at this.  So I believe this will get me my file extension and send the email out.  Not working quite right.  Any assistance would be greatly appreciated.

Dim fso, path, strExtension, myFile
Set fso = CreateObject("Scripting.FileSystemObject")
For Each myFile in fso.GetFolder("Folder_Path").Files
 strFile = myFile.Name

 strExtension = Right(strFile,1)
 If strExtension = "A" Then
SendMail "Extension A exists"
End If

Sub SendMail(strBody, result)
  YourSMTPServer = "smtpservername"
  Recipient = "Email@email.com
  'Recipient = "test@email.com"
  Sender = "test@email.com"

set iMsg = CreateObject("CDO.Message")

iMsg.Subject = strSubject
iMsg.From = Sender 
iMsg.To= Recipient
iMsg.TextBody = strBody

  iMsg.Configuration.Fields.Item _ 
("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 
iMsg.Configuration.Fields.Item _ 
("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "SMTP Server"
iMsg.Configuration.Fields.Item _ 
 ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25 
iMsg.Configuration.Fields.Update 

if result = "True" then 
iMsg.Fields.Item("urn:schemas:mailheader:X-MSMail-Priority") = "High"
iMsg.Fields.Item("urn:schemas:mailheader:X-Priority") = 2
iMsg.Fields.Item("urn:schemas:httpmail:importance") = 2
iMsg.Fields.Update
end if

iMsg.Send
  
'wscript.echo "Mail sent."
 
End Sub 

Open in new window

0
Comment
Question by:WeberTech
[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
  • 4
  • 3
7 Comments
 
LVL 3

Expert Comment

by:DEVLitehouse
ID: 39818423
It looks like you are missing the "Next" statement at line 10. Good luck!
0
 
LVL 1

Author Comment

by:WeberTech
ID: 39818488
I added that in and now its getting an error on line 25.
0
 
LVL 3

Expert Comment

by:DEVLitehouse
ID: 39818859
I cleaned up your code a bit, and tested it. As long as you change the variable values to match your environment, it should work the way you want. :)

Dim fso, path, strExtension, myFile

Set fso = CreateObject("Scripting.FileSystemObject")
For Each myFile in fso.GetFolder("Folder_Path").Files
	strFile = myFile.Name
	strExtension = Right(strFile,1)
	If strExtension = "A" Then
		SendMail "Extension A exists", "false"
	End If
Next

Sub SendMail(strBody, result)
	strSMTPServer = "smtp.example.com"
	strSMTPServerPort = 25
	strFrom = "EmailFrom@example.com"
	strTo = "EmailTo@example.com"
	strSubject = "Your email's subject"

	set iMsg = CreateObject("CDO.Message")

	iMsg.Subject = strSubject
	iMsg.From = strFrom 
	iMsg.To = strTo 'This can be a semicolon delimited list of addresses (with no spaces)
	iMsg.TextBody = strBody

	iMsg.Configuration.Fields.Item _ 
	("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 
	iMsg.Configuration.Fields.Item _ 
	("http://schemas.microsoft.com/cdo/configuration/smtpserver") = strSMTPServer
	iMsg.Configuration.Fields.Item _ 
	("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = strSMTPServerPort 
	iMsg.Configuration.Fields.Update 

	if result = "True" then 
		iMsg.Fields.Item("urn:schemas:mailheader:X-MSMail-Priority") = "High"
		iMsg.Fields.Item("urn:schemas:mailheader:X-Priority") = 2
		iMsg.Fields.Item("urn:schemas:httpmail:importance") = 2
		iMsg.Fields.Update
	end if

	iMsg.Send

	'wscript.echo "Mail sent."

End Sub 

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:WeberTech
ID: 39818949
Perfect!  Yes, it worked!  Is there a way to capture the file name with the A extension into the email?  And is there a way to scan 2 different folders?
0
 
LVL 1

Author Comment

by:WeberTech
ID: 39819401
I got the script to do what I want it to do now thanks to DEVLitehouse.  Now its sending the full file name including the file extension.  I found this function that gets the base file name.  GetTheBase = fso.GetBaseName(filespec).  How can I use it with my code?

Dim fso, path, strExtension, myFile, abendedJob

Set fso = CreateObject("Scripting.FileSystemObject")
GetTheBase = fso.GetBaseName("FolderPath")
Set abendedJob = Nothing
For Each myFile in fso.GetFolder("FolderPath").Files
	strFile = myFile.Name
	strExtension = Right(strFile,1)
	If strExtension = "A" Then
	Set abendedJob = myFile
		SendMail abendedJob & " job is abended", "false"
	End If
Next

Sub SendMail(strBody, result)
	strSMTPServer = "SMTP Server"
	strSMTPServerPort = 25
	strFrom = "email@email.com"
	strTo = "email@email.com"
	strSubject = "Job " & Right(abendedJob,8) & " is abended"

	set iMsg = CreateObject("CDO.Message")

	iMsg.Subject = strSubject
	iMsg.From = strFrom 
	iMsg.To = strTo 'This can be a semicolon delimited list of addresses (with no spaces)
	iMsg.TextBody = strBody

	iMsg.Configuration.Fields.Item _ 
	("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
	iMsg.Configuration.Fields.Item _ 
	("http://schemas.microsoft.com/cdo/configuration/smtpserver") = strSMTPServer
	iMsg.Configuration.Fields.Item _ 
	("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = strSMTPServerPort 
	iMsg.Configuration.Fields.Update 

	'if result = "False" then 
		'iMsg.Fields.Item("urn:schemas:mailheader:X-MSMail-Priority") = "High"
		'iMsg.Fields.Item("urn:schemas:mailheader:X-Priority") = 2
		'iMsg.Fields.Item("urn:schemas:httpmail:importance") = 2
		'iMsg.Fields.Update
	'end if

	iMsg.Send

	'wscript.echo "Mail sent."

End Sub 

Open in new window

0
 
LVL 3

Accepted Solution

by:
DEVLitehouse earned 500 total points
ID: 39819653
Really this should have been a second question, but I'll answer anyways.  :)
I don't know exactly what you want the output to look like. But if you wanted to change the text to be the name of the file and " job is abended" you'd change lines 6-13 to this:
For Each myFile in fso.GetFolder("FolderPath").Files
	strFile = myFile.Name
	strExtension = Right(strFile,1)
	If strExtension = "A" Then
		SendMail fso.getBaseName(myFile) & " job is abended", "false"
	End If
Next

Open in new window


However, if you just wanted to send 1 email with all of the jobs that are abended, you'd change lines 6-13 to this:

strAbendedJobs = ""
For Each myFile in fso.GetFolder("FolderPath").Files
	strFile = myFile.Name
	strExtension = Right(strFile,1)
	If strExtension = "A" Then
		strAbendedJobs = strAbendedJobs & fso.getBaseName(myFile) & vbCrLf
	End If
Next

If Len(strAbendedJobs) > 0 Then
	SendMail "The following jobs were abended: " & vbCrLf & strAbendedJobs, "false"
End If

Open in new window



I didn't test these sets of code, but they should work for you with little to no modification.
0
 
LVL 1

Author Closing Comment

by:WeberTech
ID: 39819776
Perfect!  You rock!  Exactly what I wanted.  :)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

This is an addendum to the following article: Acitve Directory based Outlook Signature (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_24950055.html) The script is fine, and works in normal client-server domains…
This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses

734 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