Link to home
Start Free TrialLog in
Avatar of bfuchs
bfuchsFlag for United States of America

asked on

Program to check for new files.

Hi Experts,
I am looking for a program that will keep checking at a particular folder every 20 minutes, if there were no new files created then will send an email.
This should be running all the time except for night hours (10.15 PM to 6.00 AM).
Thanks
Avatar of ZeropointNRG
ZeropointNRG
Flag of Australia image

Maybe something like Watch Directory? is what you're looking for?
No new files created every 20 minutes? What's the time limit it should send an email?

What about sending an email if files changed, and if you don't receive an email, it means nothing changed? Would that work?
Avatar of bfuchs

ASKER

Hi,
Files are being copied from an FTP site thru a PS script.
What's the time limit it should send an email?
Would prefer the earliest possible.
Thanks,
Ben
Hi,

I meant, at what frequency do we send an email about files NOT being altered or changed or updated? Every minute? every second? I mean, I think it would be easier if you sent email alerts ONLY when files have updated, or changed..Otherwise you have to use a set time interval to send off emails of course unless the folder is being frequently updated..
Avatar of skullnobrains
skullnobrains

On linux, you can use inotify for such tasks. There is an equivalent or identical command in most unices. No idea about windows but the command line version of filemon probably can do that.
Avatar of bfuchs

ASKER

@ZeroPoints,

Maybe something like Watch Directory?

Emailed to them, and this is what they responded,

@Hello Ben,

While WatchDirectory is not typically used for that scenario, there is a way to do this.

I wrote a blog about this some time ago, see http://blog-en.gdpsoftware.com/2010/02/receive-email-when-no-files-are-created.html

In your case you set the FILENEW, QUIET (1200 seconds) AND FILECHNG options on "Events".

To prevent alerts in the evening, use the scheduler options: https://www.watchdirectory.net/wdhelp/help/wdnewconfigpage5.html making sure to set the option "Events outside these periods are ignored".

Guess have to check up that now...

@skullnobrains,

Am not that good in figuring out stuff, perhaps you have a ready script that does this.

FYI- I changed the strategy and just a check to see if a particular file was not updated within last 20 min and send an email would be fine.

Thanks,
Ben
see if a particular file was not updated within last 20 min and send an email would be fine

* * * * * touch /var/run/REF --date -20minutes && find /path/to/file -not -newer /var/run/REF

Open in new window


this simple cron script will output the file's name if and only if the file was not updated.
this variant will output nothing if the file does not exist at all. not sure what you need.

assuming your cron is setup correctly, any output from the command will be send to the mail address defined in the crontab
Hi Ben,
Your question piqued my interest and I wrote a program in the AutoHotkey language to do it. I'm calling the program CheckFiles.

I generalized CheckFiles beyond your stated requirements in the hopes that it will be useful to more folks. Here are the specs:

• Provides for specification of a root folder to check (variable: CheckFolder).

• Provides an option for recursion into subfolders to an unlimited depth (variable: Recurse).

• Provides an option for the size of the time interval in minutes (variable: TimeIntervalMinutes).

• Provides a "safety factor" in seconds to make sure that it doesn't miss files near the beginning of the time interval (variable: SafetyFactorSeconds).

• Provides an option for which file date to check, i.e., Accessed, Created, or Modified (variable: DateCheck).

• Has an easy-to-change IF statement that defines "night hours" (currently coded for yours, i.e., between 10.15pm and 6.00am).

• Sends an email after every check, but in your case, you'll probably want to remove two of them and send only in case (2) below. The subject of the email is CheckFiles Results. The three possibilities for the body are:

(1) It is night hours, so no check was done, in which case the email body looks like this:

CheckFiles results at 2019-08-18_22.30.26 for folder:
C:\Users\Joe\AppData\Local\Temp
Recurse into its subfolders: Yes

Night hours - did not check

(2) There were no "new" files (i.e., Accessed, Created, or Modified, depending on the option chosen) in the previous time interval, in which case the email body looks like this:

CheckFiles results at 2019-08-18_09.17.52 for folder:
C:\Users\Joe\AppData\Local\Temp
Recurse into its subfolders: No

No files Created since 2019-08-18_08.17.47

(3) There was at least one "new" file in the previous time interval, in which case the email body looks like this:

CheckFiles results at 2019-08-18_11.21.06 for folder:
C:\Users\Joe\AppData\Local\Temp
Recurse into its subfolders: Yes

Number of files Modified since 2019-08-19_10.21.01: 5

20190819111143 C:\Users\Joe\AppData\Local\Temp\firefox\parent.lock
20190819110609 C:\Users\Joe\AppData\Local\Temp\~DFCF61ED007663403E.TMP
20190819110609 C:\Users\Joe\AppData\Local\Temp\~DF9641DE5B8BD032CE.TMP
20190819110609 C:\Users\Joe\AppData\Local\Temp\dat499A.tmp
20190819110609 C:\Users\Joe\AppData\Local\Temp\dat4989.tmp

The character between the date/time and the file name is a tab.

• The program puts an icon in the system tray that represents files in a folder. Hovering over the icon shows a tray tip with the program name and Version, the time of the next check, and the full path of the folder to check (Windows limits a tray tip to 127 characters, so the folder name could be truncated if it is long). It looks like this when hovering:

User generated image
• Right-clicking on the system tray icon gives a context menu with two choices: About (the default) and Exit. The About pick shows a message box dialog with the program name (CheckFiles), the program Version (this is in a variable assigned on the second line of the script), and the Build (this is the Modified date/time stamp of the AutoHotkey source code file). The Exit pick quits/terminates the program immediately.

I'm still working on fine-tuning and testing CheckFiles, but I wanted to put its specs out there for you to make sure that it meets your needs and to see if you (or other members reading this thread) have ideas for enhancements/improvements. Regards, Joe
Avatar of bfuchs

ASKER

@skullnobrains,

From what I get the picture (after googling a little), the cron program is meant to use in Unix/Linux OS, not on a windows server (we have Win Server 2012 R2).

Is that correct?


@Joe,

Sounds great!

Will that also cover the following mentioned above?

FYI- I changed the strategy and just a check to see if a particular file was not updated within last 20 min and send an email would be fine.

Thanks,
Ben
Hi Ben,

> Sounds great!

I'm glad you think so.

> Will that also cover the following mentioned above?

I missed seeing that while I was working on the program. Yes, it could easily handle that. Since you're saying "a particular file" and "updated" (not "created"), I would change the AutoHotkey script to (1) check only that particular file and (2) look at both the Created date and the Modified date.

I'll be AFK for a few hours. Will check back into the thread when I'm online again. Regards, Joe
Avatar of bfuchs

ASKER

Hi Joe,
Good, let me know when do you expect to finish it.
Thanks,
Ben
Hi Ben,

Revising it right now from CheckFiles into CheckFile (singular) to: (1) check just the particular file and (2) send the email only when the particular file has NOT been updated (created or modified) in the preceding time interval. Code already written...testing is in progress.

I recollect from past threads that you already have AutoHotkey installed, but if not, follow the instructions in my EE AutoHotkey - Getting Started article. Regards, Joe
ASKER CERTIFIED SOLUTION
Avatar of Joe Winograd
Joe Winograd
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Nice script, Joe.
Hi Z,
Thanks for the compliment and solution endorsement...both very much appreciated! And now it really is bed time. :) Regards, Joe
NP, this will come in handy for myself ;)
Am not that good in figuring out stuff, perhaps you have a ready script that does this.

From what I get the picture (after googling a little), the cron program is meant to use in Unix/Linux OS, not on a windows server (we have Win Server 2012 R2).

glad i did not spend time providing a script.

good to see you got a working m$ solution ; sorry i did not gather that from the topics.
> this will come in handy for myself

Glad to hear it.
Avatar of bfuchs

ASKER

Hi Joe,

Sorry for asking these questions...

CheckFile results at 2019-08-20_02.01.52 for file:
c:\temp\test.xlsx

Not sure how exactly I'm supposed to be calling it, from command line? just name of program?
If I am planning to have this running all the time, what do I supposed to do?
What about skipping night hours, where do I set this up?

Thanks,
Ben
Hi Ben,

> Sorry for asking these questions...

No worries...ask away!

> Not sure how exactly I'm supposed to be calling it, from command line? just name of program?

After downloading the AutoHotkey script attached to my previous post, you'll have a file called CheckFile.ahk (if you don't like the name, rename it to whatever you want, but be sure to keep the file type as AHK). It is a plain text file that is the source code of the script. Open it with your fav text editor (Notepad, Notepad++, UltraEdit, whatever) and change these variables to what you need:

FileToCheck
EmailFrom
EmailTo
EmailPassword

If you're not using a Gmail account to send the email, you'll need to make other changes, such as smtpserver, smtpserverport, etc. Save the file after you change it, remembering to make the file extension AHK (not TXT).

After a standard installation of AutoHotkey, it will own the file extension AHK, so a simple double-click (or Enter key) on the script file in your your fav file manager (File Explorer, Windows Explorer, Directory Opus, Total Commander, whatever) will run it. You can also run it on the command line, in a batch file, via the Task Scheduler, in a command prompt, in another program/script, etc.

Also, you may want to compile it into a stand-alone executable (an EXE file) so that it can run on any Windows computer without having to install AutoHotkey. My EE AutoHotkey article explains how to do that.

> If I am planning to have this running all the time, what do I supposed to do?

I already built it to run all the time, i.e., after you execute it, it will run forever...until you right-click its system tray icon and select Exit from the context menu.

> What about skipping night hours, where do I set this up?

There's nothing for you to set up...I already built it to skip night hours (look at lines 37-41 in the script).

Regards, Joe
Avatar of bfuchs

ASKER

Hi Joe,

Can you see why the attached did not run.

Thanks,
Ben
CheckFileJoe.ahk
These are not valid email addresses:

EmailFrom:="MyEmail"
EmailTo:="MyEmail"

I would expect something like this:

EmailFrom:="BenFuchsCheckFile@gmail.com"
EmailTo:="Ben@FuchsConsulting.com"

Of course, you should not post real email addresses here...so, did you have valid email addresses in the actual script that you ran?

Also, let me know if you have a system tray icon that looks like this when you hover on it:

User generated image
You should always have that when CheckFile is running. Regards, Joe
Avatar of bfuchs

ASKER

Hi,

Yes I do have that system tray.

Also I used valid credentials, just didn't post here...

Thanks,
Ben
Change the next-to-last line in the script to this:

pmsg.Send()

In other words, remove the Try in front it. That way, the script will crash if the Send doesn't work...and that's what I want to know...if the Send is failing.
Avatar of bfuchs

ASKER

Nothing is happening, see attached.

Thanks,
Ben
Untitled.png
The file you're checking is C:\Temp\Untitled.png - has that been created or modified in the last 20 minutes?
Avatar of bfuchs

ASKER

No, See attached.

Thanks,
Ben
Untitled.png
Avatar of bfuchs

ASKER

Do I have to open Outlook in order for it to work?

Thanks,
Ben
Ben,

You changed line 94...that's a problem. It was:

pmsg:=ComObjCreate("CDO.Message")

You must have accidentally deleted the "p", because in the script that you posted it is:

msg:=ComObjCreate("CDO.Message")

Regards, Joe
Avatar of bfuchs

ASKER

Good catch,

You right, while adding my credentials I accidentally deleted it.

However there must be another issue as well, as this is still not working...

Thanks,
Ben
Avatar of bfuchs

ASKER

Okay, I finally got an error message, see attached.

Thanks,
Ben
Untitled.png
> Do I have to open Outlook in order for it to work?

It shouldn't have anything to do with Outlook. The issue is the SMTP server, which you've configured as "smtp.office365.com" and "smtpserverport:=587". I've never used the SendEmail subroutine with Office 365 as the SMTP server. It works perfectly with Gmail, with my personal email provider, and with my company email provider, but I can't speak to Office 365.
Avatar of bfuchs

ASKER

This is what I saw at google.

Image result for smtp server office 365docs.microsoft.com
To send emails using Office365 server enter these details:
SMTP Host: smtp.office365.com. SMTP Port: 587. SSL Protocol: OFF. TLS Protocol: ON. SMTP Username: (your Office365 username) ...
POP3 Host: outlook. office365.com. POP3 Port: 995. TLS Protocol: ON. ...
IMAP Host: outlook. office365.com. IMAP Port: 993. Encryption: SSL.

Do you think I have to change some settings?

Thanks,
Ben
> I finally got an error message, see attached.

That's what I suspected...the Send is not working. It has something to do with the Office 365 email config that you set up, but I'm not familiar enough with that to help you. To make sure everything else in the script is working, I suggest signing up for a free Gmail account and using the config for Gmail that is in my original script. That should take you less than a half-hour and will at least confirm that the script works except for being able to configure Office 365 email properly.
Avatar of bfuchs

ASKER

Hi Joe,

I would prefer using Outlook for this, so I guess will close the question and open a new one how to make your script work on Office365...

Thanks,
Ben
Avatar of bfuchs

ASKER

Thank you!
> I would prefer using Outlook for this

To be clear, it's not an issue of using Outlook for this...the issue is using the Office 365 SMTP server. And I was not suggesting that you use Gmail permanently...only as a way to make sure that all other aspects of the script are working well for you.
Thanks for the link to the new question...that was a good idea! I've never tried to get that subroutine to work with the Office 365 mail servers...I'll give it a try.