 |
SoftTree Technologies
Technical Support Forums
|
|
Author |
Message |
mlr0911
Joined: 13 Jul 2007 Posts: 35 Country: United States |
|
Code Question |
|
I am trying to execute the code below to "watch" for a file. I am trying to add some additional criteria into the code...........notfileExists "y://test.txt", wait 30 seconds.
How can I do this? Everytime I try to execute the code w/o the file being in the y:// directory, it errors off.
Thanks in advance for your help.
 |
 |
Dim file_handle, number
Dim wait_time, number
Dim timeout, boolean
FileOpen "y://test.txt", "StreamMode", "Read", True, file_handle
notfileExists "y://test.txt", wait_time
LoopUntil file_handle, END_LOOP
// check if we are waiting already too long (more than 10 minutes)
isGreater wait_time, 600, timeout
IfThen timeout, TIMEOUT
// Notify system admin.
//MailSend "Exchange Settings", "pass", "sysadmin@mycompany.com", &
// "File watch job failed", "Giving up while waiting for myfile.txt"
exit
TIMEOUT:
// wait 30 seconds then try again
Wait 30
Add wait_time, 30, wait_time
// try opening file
FileOpen "y://test.txt", "StreamMode", "Read", True, file_handle
END_LOOP:
// Release file
FileClose file_handle
// ...
// perform main operation here
// ... |
|
|
Tue Oct 02, 2007 11:08 am |
|
 |
SysOp
Site Admin
Joined: 26 Nov 2006 Posts: 7948
|
|
|
|
You should use simple FileExists instead of FileOpen/Read/Close. The complete example is available in the on-line help in the FileExists topic. When in this topic, please click the Examples button on the help system toolbar.
|
|
Tue Oct 02, 2007 11:53 am |
|
 |
mlr0911
Joined: 13 Jul 2007 Posts: 35 Country: United States |
|
|
|
Is it possible to combine the FileExists and FileOpen/Read/Close code? (So far I have tried, but I don't think I am doing it correctly).
B/C what I need is this:
1. Check to see if the file is in Y:\\
2. If it isn't there keep watching (like every 3 minutes).
3. When found, execute the "File Watch"
4. When files finish, then I will kick off my FTP code.
Thanks.
|
|
Tue Oct 02, 2007 2:13 pm |
|
 |
mlr0911
Joined: 13 Jul 2007 Posts: 35 Country: United States |
|
|
|
I have it waiting for a file, but how would I loop until it sees it? I have it waiting for 30 seconds.
 |
 |
Dim found, boolean
Dim not_found, boolean
Dim file_handle, number
Dim wait_time, number
Dim timeout, boolean
FileExists("y:\\test.txt", found )
IfThen(not_found, Wait_time )
Wait_time:
//LoopUntil
Wait 30
Add wait_time, 30, wait_time
IfThen( found, RunFileWatch )
// main processing here...
// ...
exit
RunFileWatch:
messagebox "File Run Initiated"
FileOpen "y://test.txt", "StreamMode", "Read", True, file_handle
LoopUntil file_handle, END_LOOP
// check if we are waiting already too long (more than 10 minutes)
isGreater wait_time, 600, timeout
IfThen timeout, TIMEOUT
// Notify system admin.
MailSend "Exchange Settings", "pass", "sysadmin@mycompany.com", &
"File watch job failed", "Giving up while waiting for myfile.txt"
exit
TIMEOUT:
// wait 30 seconds then try again
Wait 30
Add wait_time, 30, wait_time
// try opening file
FileOpen "y://test.txt", "StreamMode", "Read", True, file_handle
END_LOOP:
// Release file
FileClose file_handle
// ...
// perform main operation here
// ...
mailsend "test area","","mriggs@crash.com","FTP Transfer Successful","This is an automatic notification"
|
|
|
Tue Oct 02, 2007 2:29 pm |
|
 |
SysOp
Site Admin
Joined: 26 Nov 2006 Posts: 7948
|
|
|
|
I am not really sure what you are trying to do. You cannot really open a file and wait for some other process to write something to that file. You can only open that file either before or after.
What you want likely can be done using regular file watch job. Not only you can use the GUI to point to the file that you want to monitor, but you can also make the job to wait for file size to become stable before launching your "main operation."
If you want to notify somebody that the file hasn't 'been received in time, create another job whose only purpose is to run once a day, perhaps 10 minutes after the last hope, and check whether the file watch job ran today. If not, send an email to whoever you want to notify. To make these 2 jobs work in tandem, make the "main operation" or the job itself save last run time value in some .INI file or the registry. Make the second job read that run-time value and if that value not equal today's date, email notification.
In the first job, in the very beginning put
 |
 |
IniFileSetKey "win.ini", "Jobs", "LastRun", "@T"yyyy-mm-dd"" |
In the second job use
 |
 |
Dim LastRun, string
IniFileGetKey "win.ini", "Jobs", "LastRun", LastRun
NotEqual LastRun, "@T"yyyy-mm-dd"", SendNotification
Exit
SendNotification:
MailSend
and so on |
|
|
Tue Oct 02, 2007 3:03 pm |
|
 |
mlr0911
Joined: 13 Jul 2007 Posts: 35 Country: United States |
|
|
|
This is some code snippits that I found on your website, are you telling me it won't work? I was trying to get example 2 to work for me.
Thanks for your help.
 |
 |
Implementing true "file watch".
24x7 Scheduler allows to setup a job that starts when one or more specified files (semaphores) exist. However, in some cases it is not enough to simply detect if a file exist. Some programs create files first then continue writing to the files so such files are "visible" to other programs (can be detected) before writing is complete. To overcome this limitation, you can code a job that check file sizes before performing main action. If the file size is less than average then the job can "sleep" awaiting completion. A more reliable but not always acceptable way is to try to open the file for the read operation by using FileOpen statement. If the file is being used by the originating process (still locked), the FileOpen fails and the job continues waiting, otherwise it releases the file and performs main action.
Example 1:
Dim file_size, number
Dim too_small, boolean
Dim wait_time, number
Dim timeout, boolean
CHECK_FILE_SIZE:
FileSize "myfile.txt", file_size
// check is file size is less than 1Mb ( 1048576 bytes )
isLess file_size, 1048576, too_small
IfThen too_small, TRY_LATER
// check if we are waiting already too long (more than 10 minutes)
isGreater wait_time, 600, timeout
IfThen timeout, TIMEOUT
// Notify system admin.
MailSend "Exchange Settings", "pass", "sysadmin@mycompany.com", &
"File watch job failed", "Giving up while waiting for myfile.txt"
exit
TIMEOUT:
// wait 30 seconds then try again
Wait 30
Add wait_time, 30, wait_time
GoTo CHECK_FILE_SIZE
TRY_LATER:
// OK, file size is above average, wait extra 10 seconds then try again
Wait 10
// ...
// perform main operation here
// ...
Example 2:
Dim file_handle, number
Dim wait_time, number
Dim timeout, boolean
FileOpen "myfile.txt", "StreamMode", "Read", True, file_handle
LoopUntil file_handle, END_LOOP
// check if we are waiting already too long (more than 10 minutes)
isGreater wait_time, 600, timeout
IfThen timeout, TIMEOUT
// Notify system admin.
MailSend "Exchange Settings", "pass", "sysadmin@mycompany.com", &
"File watch job failed", "Giving up while waiting for myfile.txt"
exit
TIMEOUT:
// wait 30 seconds then try again
Wait 30
Add wait_time, 30, wait_time
// try opening file
FileOpen "myfile.txt", "StreamMode", "Read", True, file_handle
END_LOOP:
// Release file
FileClose file_handle
// ...
// perform main operation here
// ...
|
|
|
Tue Oct 09, 2007 3:40 pm |
|
 |
SysOp
Site Admin
Joined: 26 Nov 2006 Posts: 7948
|
|
|
|
In this example, the script attempts to open a file, and if the file is still in use (e.g. locked), it waits for 10 minutes and tries again. FileOpen here is used for testing file lock state, not for opening and waiting. In the example, the file is read as soon as it can be successfully opened.
You are trying to open the file first and then wait for some other process to write to it. In fact, if the file exists and you open it before the other process, that other process might be unable to write to the file.
Hope this helps.
|
|
Wed Oct 10, 2007 3:31 am |
|
 |
mlr0911
Joined: 13 Jul 2007 Posts: 35 Country: United States |
|
|
|
When I am running this example code, I am getting an error on line 5: "Error Opening file." Shouldn't it loop and wait until it is able to open the file successfully? I haven't inserted my main operation yet because I want to make sure this is able to wait until the file is successful. I inserted a messagebox to let me know when it has completed successfully (when the mail operation would actually kick off).
[/img]
 |
 |
Dim file_handle, number
Dim wait_time, number
Dim timeout, boolean
FileOpen "X:\\ckl_.txt", "StreamMode", "Read", True, file_handle
LoopUntil file_handle, END_LOOP
// check if we are waiting already too long (more than 10 minutes)
isGreater wait_time, 600, timeout
IfThen timeout, TIMEOUT
// Notify system admin.
MailSend "Exchange Settings", "pass", "sysadmin@mycompany.com", &
"File watch job failed", "Giving up while waiting for myfile.txt"
exit
TIMEOUT:
// wait 30 seconds then try again
Wait 30
Add wait_time, 30, wait_time
// try opening file
FileOpen "X:\\ckl_.txt", "StreamMode", "Read", True, file_handle
END_LOOP:
// Release file
FileClose file_handle
// ...
// perform main operation here
// ...
messagebox "Done" |
|
|
Wed Oct 10, 2007 2:09 pm |
|
 |
SysOp
Site Admin
Joined: 26 Nov 2006 Posts: 7948
|
|
|
|
You need to set the job to ignore errors using the ignore errors job property or even better enter OnErrorResumeNext as the first line of the job script.
|
|
Wed Oct 10, 2007 6:15 pm |
|
 |
mlr0911
Joined: 13 Jul 2007 Posts: 35 Country: United States |
|
|
|
Makes sense, I got it to work......thanks for your help......
Since I have 3 seperate files that their name changes month by month, can I use a wildcard? If so, how can I code it with fileexists?
 |
 |
Fileexists ("x:\\changesmade1_*.txt",found)
and FileOpen ("x:\\changesmade1_*) etc
Example:
Current Names
changesmade1_20071010.txt
changesmade2_20071009.txt
changesmade3_20071008.txt
Could I use a wild card:
changesmade1_*.txt
changesmade2_*.txt
changesmade3_*.txt |
|
|
Thu Oct 11, 2007 9:41 am |
|
 |
mlr0911
Joined: 13 Jul 2007 Posts: 35 Country: United States |
|
|
|
I got *? to work on the FileExists statement but not the FileOpen statement.
How would I get this to work on the FileOpen statement?
Thanks as always for your help.
|
|
Thu Oct 11, 2007 10:23 am |
|
 |
mlr0911
Joined: 13 Jul 2007 Posts: 35 Country: United States |
|
|
|
Is this possible to do?
Is there another way to do this if this isn't possible?
Thanks
|
|
Thu Oct 11, 2007 1:37 pm |
|
 |
SysOp
Site Admin
Joined: 26 Nov 2006 Posts: 7948
|
|
|
|
Yes, it is surely possible. FileExists, DirEx or Dir can be used with wildcards. Yet, to open a file with FileOpen you need to specify the complete file name which you can get using the DirEx statement. You just need to add an extra line to pick the file name, for example
 |
 |
Dim( files, string )
DirEx ("x:\\changesmade1_*.txt", files ) |
and add some logic to deal with situations when there are multiple files in the same directory that match the specified file mask, for example
 |
 |
Dim( many_files, int )
InStr( files, ",", many_files)
IfThen( many_files, NOTIFY_ME )
main business logic here
Exit
NOTIFY_ME:
MailSend(
. Whatever
., "Error: multiple TXT files for this job have been found in the monitored directory")
|
|
|
Thu Oct 11, 2007 2:33 pm |
|
 |
mlr0911
Joined: 13 Jul 2007 Posts: 35 Country: United States |
|
|
|
Great!!! Thanks for your help.
|
|
Thu Oct 11, 2007 4:11 pm |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|
|