Solved

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

Posted on 2011-03-15
19
791 Views
Last Modified: 2012-05-11
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?
0
Comment
Question by:CCG3
[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
  • 11
  • 6
  • 2
19 Comments
 
LVL 10

Expert Comment

by:peetm
ID: 35140304
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
 

Author Comment

by:CCG3
ID: 35140684
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
 
LVL 65

Accepted Solution

by:
RobSampson earned 500 total points
ID: 35143601
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
Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

 

Author Comment

by:CCG3
ID: 35171380
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
 

Author Comment

by:CCG3
ID: 35172353
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
 

Author Comment

by:CCG3
ID: 35172420
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
 
LVL 10

Expert Comment

by:peetm
ID: 35172639
Does this help? Just a simple sub ...

http://www.motobit.com/tips/detpg_send-email-from-asp/
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 35174750
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
 

Author Comment

by:CCG3
ID: 35181847
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
 

Author Comment

by:CCG3
ID: 35181881
and I got the email with the same Error. So that change does work.. But it means something is failing somewhere.
0
 

Author Comment

by:CCG3
ID: 35192193
any suggestions on how to troubleshoot to see what required object is not there?
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 35194341
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
 

Author Comment

by:CCG3
ID: 35202089
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
 
LVL 65

Expert Comment

by:RobSampson
ID: 35202852
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
 

Author Comment

by:CCG3
ID: 35203498
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
 
LVL 65

Expert Comment

by:RobSampson
ID: 35203812
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
 

Author Comment

by:CCG3
ID: 35206467
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
 

Author Closing Comment

by:CCG3
ID: 35211208
This was a life saver for me. I couldn't ask for a better solution! Thanks for the help!!
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 35211405
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

Featured Post

Free NetCrunch network monitor licenses!

Only on Experts-Exchange: Sign-up for a free-trial and we'll send you your permanent license!

Here is what you get: 30 Nodes | Unlimited Sensors | No Time Restrictions | Absolutely FREE!

Act now. This offer ends July 14, 2017.

Question has a verified solution.

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

A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
Postmortem reporting allow us to examine mistakes in a way that focuses on the situational aspects of a failure’s mechanism and the decision-making process of individuals proximate to the failure. Read our guide on how to handle IT post-mortem repor…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

691 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