How to poll for files in an unattended application

Tony Marston - 13th December 2000
Amended - 21st November 2001

If you have a third party application which creates data files that have to be processed by your system there are two ways in which this can be done:

This document describes how to create an application which runs in the background, wakes up every so often looking for files to process, then goes back to sleep again. It will keep doing this until told to stop.

This utility makes use of the $timeout function which fires the <ASYNC INTERRUPT> trigger after a specified period of inactivity. As the timeout interval can only be expressed in whole minutes if something smaller is required you will have to use another method. If you would like details of an alternative method I have created which allows intervals as small as 1 second then please refer to my follow-up article Unattended File Polling with short Intervals.

The sample code which provides a working example of what is described in this document contains the following components:

1. Startup Shell
This activates form POLL_002 and contains in its <ASYNC INTERRUPT> trigger the single command macro "^ACCEPT". This fires the <ACCEPT> trigger of the current form (which should be POLL_002) when each timeout interval expires.
2. Form POLL_001
Once started the application should be left at this screen, which may be minimised. It shows the name of the folder that will be polled, plus the file mask which identifies which files to look for. The values are obtained from the [logicals] section in the assignment file, but can be overridden at run time.This screen just sits there waiting for the <ACCEPT> trigger to be fired, either by the unattended timeout or by manual intervention. When this trigger is fired control is passed to the next form, POLL_002. If the CLOSE button is pressed the application will terminate.
3. Form POLL_002
This takes a sniff at the contents of the designated folder by issuing a DIR command. If it finds any files that match the specified file mask it loads them in, processes them, then deletes them one at a time. How you actually process the files is up to you.

Note that when a timeout event is fired the clock does not start ticking for the next interval until all processing has been completed and keyboard input is requested. This means that POLL_002 cannot be re-activated while the previous activation is still processing. The periods between each unattended activation will be equal to the timeout interval.


For those of you who are too lazy to download my sample code here is the portion which obtains the directory listing. Note that for Windows NT it is possible to use the activate command, while for Windows 95 you have to use the spawn command instead. This is due to bug #19136 which has been outstanding for several years.

params
   string pi_InputString   : IN
endparams
variables
   string lv_OprSys
   string lv_FileList
endvariables

getlistitems/id/local pi_InputString    ; load into local variables

lv_OprSys = $oprsys             ; get id of operating system

; look for any files existing in the IN directory =========================
if (lv_OprSys = "L")            ; operating system = NT
   $1 = "cmd.exe /c DIR /b /on %%$inpath$%%$file_mask$"
   activate "OS".commandout($1,lv_FileList)
   if ($procerror)
      call PROC_ERROR($procerrorcontext)
      return(-1)
   endif
else                            ; operating system not = NT
   $1 = "set dircmd=/ogn%%^"    ; do not pause for large list
   $1 = "%%$1dir /b /on %%$inpath$%%$file_mask$ > %%$inpath$filelist.txt"
   $1 = "%%$1%%^cls"            ; append 'cls' to close window

   $2 = "%%$inpath$doscmnd.bat"
   file_dump $1,"%%$2"          ; create batch file to obtain list
   if ($status < 0) return(-1)

   spawn "#%%$2"                ; execute this file
   if ($status < 0) return(-1)

   file_load "%%$inpath$filelist.txt",lv_FileList  ; load list
endif

if (lv_FileList != "")
   call SCAN_FILE_LIST(lv_FileList)         ; process the list
endif

if ($delete_list$ != "")                    ; if list not empty
   call DELETE_FILES($delete_list$)         ; delete input file(s)
endif

Tony Marston
13th December 2000

mailto:tony@tonymarston.net
mailto:TonyMarston@hotmail.com
http://www.tonymarston.net

Amendment history:

21st Nov 2001 Added reference to follow-up article Unattended File Polling with short Intervals

Back to TOP.

counter