Monitor a folder and email when new files are added....

Is it possible to have a batch or script that will monitor a folder and send an email once a file is created in the folder?
CCG3Sr Systems AdministratorAsked:
Who is Participating?
 
RobSampsonCommented:
Check out the script here....it should fit pretty well:
http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_26794886.html

Regards,

Rob.
0
 
peetmCommented:
As far as I know, you'd have to have Windows exec the script from time-to-time to so that; or have the script loop forever -- which is not nice.

You should be able to do it very nicely with a .NET language though; as it has the in-built ability to 'watch a folder for changes :

http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.aspx
0
 
CCG3Sr Systems AdministratorAuthor Commented:
Thanks for the info peetm.

I was hoping someone had already done this with a .bat or a .vbs, or something and could share how they handled it.
0
Protect Your Employees from Wi-Fi Threats

As Wi-Fi growth and popularity continues to climb, not everyone understands the risks that come with connecting to public Wi-Fi or even offering Wi-Fi to employees, visitors and guests. Download the resource kit to make sure your safe wherever business takes you!

 
CCG3Sr Systems AdministratorAuthor Commented:
Thanks, Rob! this looks very promising but I haven't been able to actually test with it yet. I didn't see where it would email if something changed. Did I overlook it? Once you have this setup can I just create it as a task to run once an hour?
0
 
CCG3Sr Systems AdministratorAuthor Commented:
Ok. Now that I have saved the code and gave it a quick look I do see where it should email. I am trying to test with it now. Not doing so good so far and not sure what is wrong with it yet.
0
 
CCG3Sr Systems AdministratorAuthor Commented:
Ok, this looks like it works pretty good but I am unable to get the email part to work. I have a feeling it wants to send to an email server (server. my-domain.com) but we outsource our email so we don't have one in-house. I do have a server running the SMTP service and I know it works fine. But when I just enter in that IP for the value of strSMTP= it doesn't work.

I think it is trying to use Outlook to create a message and send it via Outlook and I don't think that will work. Is there a way to use this with the SMTP server that we have in house?

Thanks again for your help Sam! This a great code!
0
 
peetmCommented:
Does this help? Just a simple sub ...

http://www.motobit.com/tips/detpg_send-email-from-asp/
0
 
RobSampsonCommented:
It doesn't use outlook, it uses just SMTP messages.  It's also supposed to send an email to the administrator with this section:
If Err.Number <> 0 or MailErr <> 0 Then
    SendEmail "me@company.com; you@company.com", _
              "FTPScriptError@cclind.com", _
              "FTP Monitor Scripting Error", _
              "<b>Error Num.: </b>" & Err.Number & "<br>" & _
                "<b>Error Desc.: </b>" & Err.Description & "<br>" & _
                "<b>Error Src.: </b>" & Err.Source, _
              strSMTPServer
    Err.Clear
End If

Open in new window


if the first email fails, but that of course, won't do you much good if it's not working altogether.  So, change that above section to this:
If Err.Number <> 0 or MailErr <> 0 Then
    MsgBox "Email Error: " & Err.Number & vbCrLf & _
                "Error Description: " & Err.Description & vbCrLf & _
                "Error Source: " & Err.Source
    SendEmail "me@company.com; you@company.com", _
              "FTPScriptError@cclind.com", _
              "FTP Monitor Scripting Error", _
              "<b>Error Num.: </b>" & Err.Number & "<br>" & _
                "<b>Error Desc.: </b>" & Err.Description & "<br>" & _
                "<b>Error Src.: </b>" & Err.Source, _
              strSMTPServer
    Err.Clear
End If

Open in new window


and then you should see a MsgBox that states the error, which should help us track down the problem.

Regards,

Rob.
0
 
CCG3Sr Systems AdministratorAuthor Commented:
Yes now that gives me an error.

Email Error: 424
Error Description: Object required
Error Source: Microsoft VBScript runtime error.

It doesn't really say what object is missing.
0
 
CCG3Sr Systems AdministratorAuthor Commented:
and I got the email with the same Error. So that change does work.. But it means something is failing somewhere.
0
 
CCG3Sr Systems AdministratorAuthor Commented:
any suggestions on how to troubleshoot to see what required object is not there?
0
 
RobSampsonCommented:
You're right, that really doesn't give us any hints.  The easiest way to do it is to comment out On Error Resume Next, and run it again.  Then the script will error out with a line number.

Regards,

Rob.
0
 
CCG3Sr Systems AdministratorAuthor Commented:
Ok so I removed the entire block for the On Error and the script runs but nothing is emailed (of course) and no errors come up. The txt file is updated with the files that changed though. So I guess the error is in this block of code?

If Err.Number <> 0 or MailErr <> 0 Then
    MsgBox "Email Error: " & Err.Number & vbCrLf & _
                "Error Description: " & Err.Description & vbCrLf & _
                "Error Source: " & Err.Source
    SendEmail "myemail@mycompany.com; myemail@mycompany.com", _
              "myemail@mycompany.com", _
              "FTP Monitor Scripting Error", _
              "<b>Error Num.: </b>" & Err.Number & "<br>" & _
                "<b>Error Desc.: </b>" & Err.Description & "<br>" & _
                "<b>Error Src.: </b>" & Err.Source, _
              strSMTPServer
    Err.Clear
End If
0
 
RobSampsonCommented:
I didn't mean remove the entire block....just comment out the single On Error Resume Next line, and then run the code as normal.  Then, instead of getting the MsgBox for the error, you should see a Windows Script Host dialog with the error line as well.

Rob.
0
 
CCG3Sr Systems AdministratorAuthor Commented:
Oh ok. I have put that code back in.

I am getting the following Error:
Line:  137
Char:  17
Error:  Object required: 'objSubFolder'
Code:  800A01A8

It looks like we are setting it to nothing and then not giving it a value before it is used. I don't have any sub folders right now but I could so I can see the benefit of looking at them.


0
 
RobSampsonCommented:
OK, that makes more sense.  change the four instances of this line:
                Call AddSubFolderHeader(objSubFolder.Name)

to this
                Call AddSubFolderHeader(objFSO.GetFolder(strFolderPath).Name)


and see how that goes.

Regards,

Rob.
0
 
CCG3Sr Systems AdministratorAuthor Commented:
Ok now everything looks good. The only funny thing I see right now is that if I change the name of a file it shows the new name as being created and deleted in the email report. But I don't see that as a show stopper.

Rob I can't express my gratitude enough for your help. I really appreciate it! I hope I can pay it forward very soon! Thanks again!
0
 
CCG3Sr Systems AdministratorAuthor Commented:
This was a life saver for me. I couldn't ask for a better solution! Thanks for the help!!
0
 
RobSampsonCommented:
No problem. Thanks for the grade.

>> The only funny thing I see right now is that if I change the name of a file it shows the new name as being created and deleted in the email report.

This seems logical though....you're the only person that knows the new file is a renamed version of the old one.  As far as the script is concerned, anything with a different name and modified date is a different file.  It would only know if the files were the same, but renamed, if it compared the actual binary content, which is well beyond the capabilities of this script.

Regards,

Rob.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.