Is there a clean way to detect new files in a directory using BR?
I need to watch a directory and process any new files detected. Files will be processed differently based on the name of the file (ie. recipe1234 will call a recipe processing routine, rawmat1021 will call a rawmat processing routine) so, I need to be able to detect new files and read their names for processing.
Any help you can offer will be greatly appreciated!
Thanks,
Sue
Detecting new files in a directory
Moderators: Susan Smith, admin, Gabriel
-
- Posts: 9
- Joined: Mon Jun 08, 2009 7:23 pm
- Location: Northeast Ohio
DIR >DIR-[SESSION].TXT
Use the DIR command to create a text file, parse the file into a list.
Use SLEEP(.1) or longer if you like so that the process doesn't kill your server.
Using a loop like this, the routine will recognize the new file in .1 seconds.
If you are in less of a hurry, sleep for longer.
Use the DIR command to create a text file, parse the file into a list.
Use SLEEP(.1) or longer if you like so that the process doesn't kill your server.
Using a loop like this, the routine will recognize the new file in .1 seconds.
If you are in less of a hurry, sleep for longer.
-
- Posts: 717
- Joined: Sun Aug 10, 2008 4:24 am
- Location: Southern California
Sue,
Question: Are there ONLY new files in this folder? Or once files are processed, do they remain there in addition to newly added ones?
Is the file creation date adequate to determine which files are new? Can you parse a textfile that contains the directory listing of the folder that you are interested in?
As far as I know, BR's internal DIR command doesn't allow the ability to sort by date. But DOS does, so you could use:
00100 EXECUTE "SYSTEM DIR D:\DATAFILES\*.* >dirlist /O-D"
This will get your directory listing in order by the newest file first.
Then you need to open that dirlist file and parse it to get to the file detail. You'll need to toss out the heading information from the directory. Try this:
00010 EXECUTE "SYSTEM DIR D:\SR\SRDATA\*.* >dirlist /O-D"
00020 DIM A$*100,LONGFILENAME$*100
00030 OPEN #1: "Name=dirlist",DISPLAY,INPUT
00040 !
00050 LINPUT #1: A$ EOF FINISH
00060 LET A$=UPRC$(A$)
00070 IF A$(19:20)<>"AM" AND A$(19:20)<>"PM" THEN GOTO 50
00080 LET LONGFILENAME$=TRIM$(A$(40:(LEN(TRIM$(A$)))))
00090 LET FILEDATE=VAL(SREP$(A$(1:10),"/",""))
00100 ! ...do something with LONGFILENAME$ here...
00110 GOTO 50
00120 FINISH: PAUSE
If you don't want to use a system call for the Operating System DIR command, you can use BR's internal DIR command, but you won't have the ability to sort by date. If you need to process the files in order by date (and/or time), you'd have to parse the file date and file name out of the file by reading through the file (as in the code above) but use DIDX or AIDX to sort an "index" array that lets you read your original list of files in the proper date order. If you need help with that, give a holler.
If as Luis mentioned, you ONLY have new files in that folder, then the code I posted should help you parse the information and his DIR command will let you poll that folder.
-- Susan
Question: Are there ONLY new files in this folder? Or once files are processed, do they remain there in addition to newly added ones?
Is the file creation date adequate to determine which files are new? Can you parse a textfile that contains the directory listing of the folder that you are interested in?
As far as I know, BR's internal DIR command doesn't allow the ability to sort by date. But DOS does, so you could use:
00100 EXECUTE "SYSTEM DIR D:\DATAFILES\*.* >dirlist /O-D"
This will get your directory listing in order by the newest file first.
Then you need to open that dirlist file and parse it to get to the file detail. You'll need to toss out the heading information from the directory. Try this:
00010 EXECUTE "SYSTEM DIR D:\SR\SRDATA\*.* >dirlist /O-D"
00020 DIM A$*100,LONGFILENAME$*100
00030 OPEN #1: "Name=dirlist",DISPLAY,INPUT
00040 !
00050 LINPUT #1: A$ EOF FINISH
00060 LET A$=UPRC$(A$)
00070 IF A$(19:20)<>"AM" AND A$(19:20)<>"PM" THEN GOTO 50
00080 LET LONGFILENAME$=TRIM$(A$(40:(LEN(TRIM$(A$)))))
00090 LET FILEDATE=VAL(SREP$(A$(1:10),"/",""))
00100 ! ...do something with LONGFILENAME$ here...
00110 GOTO 50
00120 FINISH: PAUSE
If you don't want to use a system call for the Operating System DIR command, you can use BR's internal DIR command, but you won't have the ability to sort by date. If you need to process the files in order by date (and/or time), you'd have to parse the file date and file name out of the file by reading through the file (as in the code above) but use DIDX or AIDX to sort an "index" array that lets you read your original list of files in the proper date order. If you need help with that, give a holler.
If as Luis mentioned, you ONLY have new files in that folder, then the code I posted should help you parse the information and his DIR command will let you poll that folder.
-- Susan
-
- Posts: 717
- Joined: Sun Aug 10, 2008 4:24 am
- Location: Southern California
One other thing: An easy way to make sure you only process new files, is to move them to some other directory after you're done. You could have an "incoming" folder and an "incoming\finished" folder. I dont know if thats possible in your situation but I thought I'd throw it out there anyway just in case.
Gabriel
Gabriel
-
- Posts: 9
- Joined: Mon Jun 08, 2009 7:23 pm
- Location: Northeast Ohio