Solved

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

Posted on 2014-01-29
7
594 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
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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction During my participation as a VBScript contributor at Experts Exchange, one of the most common questions I come across is this: "I have a script that runs against only one computer. How can I make it run against a list of computers in …
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 …
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

830 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