Solved

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

Posted on 2014-01-29
7
587 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Recently I finished a vbscript that I thought I'd share.  It uses a text file with a list of server names to loop through and get various status reports, then writes them all into an Excel file.  Originally it was put together for our Altiris server…
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…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Edureka is one of the fastest growing and most effective online learning sites.  We are here to help you succeed.

911 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

19 Experts available now in Live!

Get 1:1 Help Now