Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2014-01-29
7
Medium Priority
?
629 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 2000 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

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…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

610 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