Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

File handling

Status
Not open for further replies.

aixinstall

Technical User
Oct 1, 2002
17
NL
Hello,

I have a problem, i want to automatically start a program if in some directory a file is dropt.

We have a customer who sends us a file, this file is put in a directory. On that moment "something" must detect that file is arrived, and than automatically start program XXXXX with the data from the received file. We already can run the program xxxxx with the file input. But we looking for "something" to detect the file is there and run the program.

Thanks,
 
Use cron and a find statement to periodically check whether the file has arrived:

find <dir> -name <filename>

If find returns 0 (ie $?=0), run your script to process it, if not, sleep until the next check. The problem with this approach is that you may be unlucky enough to locate the file, but at a time when the download isn't complete. You can circumvent this by sending a further (empty if you like) check file after the main file which will only exist if the transfer is complete. Again, do a $? check for this in the checking script. Delete this check file once the process is complete.

Something like:

find <dir> -name <filename>
if [ $? = 0 ]
then
find <dir> -name <checkfile>
if [ $? =0 ]
then
<do your processing>
rm <checkfile>
else
exit
fi
fi

You'll probably need to play with this (it's untested and from (possibly faulty) memory, but hope it helps as a start.
 
Hmm. Perhaps the [ $? = 0 ] should be [ $? -eq 0 ] above! Too early on Saturday morning!!
 
Sorry i've been away for a while, but thanks i will try.
 
Some of the ftp packages have event driven options, look in sourceforge.

On linux, since I had the source, I made a simple mod to the ftp daemon to spawn off the process at close up.

A major issue is the running of your program in the event that the file is still being uploaded, or if it is a corrupt file. The ideal situation is to have the ftp daemon do the event. You could try google for a replacement package.
 
Another way to do this would be using mailx, that is if you have sendmail installed. I did this once, and you can pipe the contents to your script. There is mailrc file, and when the mail arrives, it can be directed to your script/program. see man mailx.

If this is a simple text file, it should be easy, otherwise you have to see how the uuecode has to implemented to handle binary files like an excel/word file.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top