I'm trying to figure out how to write a VBScript that will read a
folder, and send an email if there is a file with an extension of
BAD.

I have a block of code that works to send the email using Outlook from
other scripts I have in place. I have no idea where to start with the
rest of the script. The files will have numerous names, but they will
only have an extension of BAD or ERR. If they are there, I want to
move them after the email is sent so I don;t continue to get false
alerts.

Any help pointing me in the right direction would be great!

Re: VBScript to send email if file is in folder by Greg

Greg
Fri Mar 14 11:30:51 CDT 2008

Hi Andrew,

Use the FileSystemObject. I suggest that you download the script56.chm file
from microsoft website, you will have a complete reference.

Use the GetExtensionName method to retrieve the extension name, then move
the file using the move method.

Do a loop for example every 5 or 10 second you rescan the folder and that's
it.

Enjoy

Greg


<andrew.beeler@gmail.com> wrote in message
news:dcd46ecc-b525-42de-aa6d-2f25ed17f104@n77g2000hse.googlegroups.com...
> I'm trying to figure out how to write a VBScript that will read a
> folder, and send an email if there is a file with an extension of
> BAD.
>
> I have a block of code that works to send the email using Outlook from
> other scripts I have in place. I have no idea where to start with the
> rest of the script. The files will have numerous names, but they will
> only have an extension of BAD or ERR. If they are there, I want to
> move them after the email is sent so I don;t continue to get false
> alerts.
>
> Any help pointing me in the right direction would be great!



Re: VBScript to send email if file is in folder by Pegasus

Pegasus
Fri Mar 14 11:52:51 CDT 2008


<andrew.beeler@gmail.com> wrote in message
news:dcd46ecc-b525-42de-aa6d-2f25ed17f104@n77g2000hse.googlegroups.com...
> I'm trying to figure out how to write a VBScript that will read a
> folder, and send an email if there is a file with an extension of
> BAD.
>
> I have a block of code that works to send the email using Outlook from
> other scripts I have in place. I have no idea where to start with the
> rest of the script. The files will have numerous names, but they will
> only have an extension of BAD or ERR. If they are there, I want to
> move them after the email is sent so I don;t continue to get false
> alerts.
>
> Any help pointing me in the right direction would be great!

Here is an example that does almost what you want to do:
http://www.microsoft.com/technet/scriptcenter/resources/qanda/jan06/hey0126.mspx

Further to Greg's recommendation, you should also download
the whole Scripting Guy help file. It contains a large number
of very useful scripts.



Re: VBScript to send email if file is in folder by Paul

Paul
Fri Mar 14 16:45:25 CDT 2008

Hi Andrew,

I remembered I once wrote something like that (before I changed my
phone-system to Asterisk). I think it does just about what you want.

Consider it PD.

Cheers,

Paul
---

'This script monitors a specified Directory (using WMI)
'If a file is created in the Directory it picks it up and sends it in an
E-Mail
'SMTP service must be running on the network somewhere.
'Author : Paul Weterings
'E-Mail : Paul[at]syncpuls.com
'Date : Feb 2004
'Version : 1.1

Const cdoSendUsingPort = 2
strComputer = "."

'You can change the below variables to adapt for your usage.

myEmail = "Paul@syncpuls.com"
myFrom = "Voicemail@syncpuls.com"
myMsg = "Open the attachment to listen to the voicemail"
mySubject = "Voicemail"
myExtension = "wav"

Dim namestr
Dim objEmail
Dim iConf
Dim Flds

set iConf = CreateObject("CDO.Configuration")
Set Flds = iConf.Fields

'Set the CDOSYS configuration fields to use port 25 on the SMTP server.

With Flds
.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") =
cdoSendUsingPort
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver")
= "mailserver.yoursite.com"

.Item("http://schemas.microsoft.com/cdo/configuration/SMTPServerPort") = 25

.Item("http://schemas.microsoft.com/cdo/configuration/SMTPAuthenticate")
= cdoAnonymous ' 0

.Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout")
= 10
.Update
End With

'Set up WMI for checking the Directory
Set objWMIService = GetObject("winmgmts:" &
"{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colMonitoredEvents = objWMIService.ExecNotificationQuery ("SELECT *
FROM __InstanceCreationEvent WITHIN 10 WHERE " & "Targetinstance ISA
'CIM_DirectoryContainsFile' and " & "TargetInstance.GroupComponent= " &
"'Win32_Directory.Name=""c:\\\\temp""'")

'Infinite loop
Do
'Monitor the directory
Set objLatestEvent = colMonitoredEvents.NextEvent

'We only want the folder and filename
namestr = Split(objLatestEvent.TargetInstance.PartComponent, "=", -1, 1)

'clean the string up, remove extra quotes and slashes
namestr(1) = Replace(Replace (namestr(1),"\\","\"),"""","")

'Only take action if we have a .wav file
If StrComp(myExtension, Right(namestr(1),3), 1) = 0 Then

'For debugging purposes.
'Wscript.Echo namestr(1)

Set objEmail = CreateObject("CDO.Message")

objEmail.Configuration = iConf
objEmail.From = myFrom
objEmail.To = myEmail
objEmail.Subject = mySubject
objEmail.Textbody = myMsg
objEmail.AddAttachment(namestr(1))
objEmail.Send

Set objEmail = Nothing
End If
Loop

Set iConf = Nothing
Set Flds = Nothing




andrew.beeler@gmail.com wrote:
> I'm trying to figure out how to write a VBScript that will read a
> folder, and send an email if there is a file with an extension of
> BAD.
>
> I have a block of code that works to send the email using Outlook from
> other scripts I have in place. I have no idea where to start with the
> rest of the script. The files will have numerous names, but they will
> only have an extension of BAD or ERR. If they are there, I want to
> move them after the email is sent so I don;t continue to get false
> alerts.
>
> Any help pointing me in the right direction would be great!

Re: VBScript to send email if file is in folder by Paul

Paul
Fri Mar 14 16:47:50 CDT 2008

Sorry, I just realized this is not what you're looking for. The script
waits for a file tot pop up in the folder, but you simply want to scan
the folder for a certain extension.

Oh well.... I should read before I post...

Paul Weterings wrote:
> Hi Andrew,
>
> I remembered I once wrote something like that (before I changed my
> phone-system to Asterisk). I think it does just about what you want.
>
> Consider it PD.
>
> Cheers,
>
> Paul
> ---
>
> 'This script monitors a specified Directory (using WMI)
> 'If a file is created in the Directory it picks it up and sends it in an
> E-Mail
> 'SMTP service must be running on the network somewhere.
> 'Author : Paul Weterings
> 'E-Mail : Paul[at]syncpuls.com
> 'Date : Feb 2004
> 'Version : 1.1
>
> Const cdoSendUsingPort = 2
> strComputer = "."
>
> 'You can change the below variables to adapt for your usage.
>
> myEmail = "Paul@syncpuls.com"
> myFrom = "Voicemail@syncpuls.com"
> myMsg = "Open the attachment to listen to the voicemail"
> mySubject = "Voicemail"
> myExtension = "wav"
>
> Dim namestr
> Dim objEmail
> Dim iConf
> Dim Flds
>
> set iConf = CreateObject("CDO.Configuration")
> Set Flds = iConf.Fields
>
> 'Set the CDOSYS configuration fields to use port 25 on the SMTP server.
>
> With Flds
> .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") =
> cdoSendUsingPort
> .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") =
> "mailserver.yoursite.com"
>
> .Item("http://schemas.microsoft.com/cdo/configuration/SMTPServerPort") = 25
>
> .Item("http://schemas.microsoft.com/cdo/configuration/SMTPAuthenticate")
> = cdoAnonymous ' 0
>
> .Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout")
> = 10
> .Update
> End With
>
> 'Set up WMI for checking the Directory
> Set objWMIService = GetObject("winmgmts:" &
> "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
> Set colMonitoredEvents = objWMIService.ExecNotificationQuery ("SELECT *
> FROM __InstanceCreationEvent WITHIN 10 WHERE " & "Targetinstance ISA
> 'CIM_DirectoryContainsFile' and " & "TargetInstance.GroupComponent= " &
> "'Win32_Directory.Name=""c:\\\\temp""'")
>
> 'Infinite loop
> Do
> 'Monitor the directory
> Set objLatestEvent = colMonitoredEvents.NextEvent
>
> 'We only want the folder and filename
> namestr = Split(objLatestEvent.TargetInstance.PartComponent, "=",
> -1, 1)
>
> 'clean the string up, remove extra quotes and slashes
> namestr(1) = Replace(Replace (namestr(1),"\\","\"),"""","")
>
> 'Only take action if we have a .wav file
> If StrComp(myExtension, Right(namestr(1),3), 1) = 0 Then
>
> 'For debugging purposes.
> 'Wscript.Echo namestr(1)
>
> Set objEmail = CreateObject("CDO.Message")
>
> objEmail.Configuration = iConf
> objEmail.From = myFrom
> objEmail.To = myEmail
> objEmail.Subject = mySubject
> objEmail.Textbody = myMsg
> objEmail.AddAttachment(namestr(1))
> objEmail.Send
>
> Set objEmail = Nothing
> End If
> Loop
>
> Set iConf = Nothing
> Set Flds = Nothing
>
>
>
>
> andrew.beeler@gmail.com wrote:
>> I'm trying to figure out how to write a VBScript that will read a
>> folder, and send an email if there is a file with an extension of
>> BAD.
>>
>> I have a block of code that works to send the email using Outlook from
>> other scripts I have in place. I have no idea where to start with the
>> rest of the script. The files will have numerous names, but they will
>> only have an extension of BAD or ERR. If they are there, I want to
>> move them after the email is sent so I don;t continue to get false
>> alerts.
>>
>> Any help pointing me in the right direction would be great!