Solved

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

Posted on 2011-03-15
19
778 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
  • 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
 

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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Transparency shows that a company is the kind of business that it wants people to think it is.
Are you one of those front-line IT Service Desk staff fielding calls, replying to emails, all-the-while working to resolve end-user technological nightmares? I am! That's why I have put together this brief overview of tools and techniques I use in o…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

706 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

18 Experts available now in Live!

Get 1:1 Help Now