Solved

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

Posted on 2014-01-29
7
584 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
  • 4
  • 3
7 Comments
 
LVL 3

Expert Comment

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

Author Comment

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

Expert Comment

by:DEVLitehouse
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 1

Author Comment

by:WeberTech
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Perfect!  You rock!  Exactly what I wanted.  :)
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
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…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

772 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

11 Experts available now in Live!

Get 1:1 Help Now