Importing files from os-dir

JvdB

Member
Hello Peggers

I'm working on an application that imports files from a directory (eventually also subdirectories).
Some of the files will be converted to one or more files in that same directory and should also be imported.

We used the following code (simplified) for this:

DEFINE VARIABLE cImportDir AS CHARACTER NO-UNDO.

RUN find-file (cImportDir).

PROCEDURE find-file :

DEFINE INPUT PARAMETER cDir AS CHARACTER NO-UNDO.

DEFINE VARIABLE c1 AS CHARACTER NO-UNDO. /*file name*/
DEFINE VARIABLE c2 AS CHARACTER NO-UNDO. /*file name with path*/
DEFINE VARIABLE c3 AS CHARACTER NO-UNDO. /*file type*/

INPUT FROM OS-DIR(cDir).
REPEAT:
IMPORT c1 c2 c3.
IF c1 = "." OR c1 = ".." THEN NEXT.
IF INDEX(c3, "D":U) = 1 THEN RUN find-file(c2).
IF INDEX(c3, "F":U) > 0 THEN RUN conversion(c2).
END.
INPUT CLOSE.
END PROCEDURE.


The procedure conversion checks the file and creates new file(s) in the same directory, and removes the original file.

Now this code seems to work when there is more then 3-4 files in the directory already. However when there is for example only one file in the directory, then the new file is not processed.
Does anybody have an explanation for this behaviour and if possible a way so that the procedure will process all files in the directory including the new ones?

We work with Progress 8.3b on windows 95/98.
 

m1_ru

New Member
As I understand new files will be created while
executing multiple import from os-dir statements.

Progress does not document behaviour
of import from os-dir statement:
sometimes it can find new files, sometimes - not.


If i would write such program, i would make it in four stages:
1. Design temp-table to store files and directories
2. Implement procedure for recursive scan of files
2a. read files and directories from start directory.
(do not include directories '.' and '..')
save information (full-path of each file and directory)
to temp-table.
Use named stream:
<pre>
define stream dir-list .
input stream dir-list from os-dir( p-dir-name ).
repeat:
import stream dir-list v-file v-path v-mask .
/* process file */
end.
input stream dir-list close .
</pre>
Scan one directory at once without calling of
other procedures .

2b. find first directory in temp-table that not processed
mark the directory as processed.
run step 2a, using selected directory as start directory.
continue to repeat step 2b until there are at least one directory
that was not processed.

At this point you have complete recursive list of files. The procedure can be tested by comparing temp-table with the recursive list of files received from some other program.

3. process the files that need to be processed from temp-table.
for each files to be splitted - remove original file from temp-table
and create records for new files.

4. import all files that are in temp-table .
 
I agree with the previous post. THis is by far the best way to do it. I've used this method for importing file information and it works really well.
 

JvdB

Member
Thnx!

Thnx for the replies guys,

I have returned to my original construction of indeed temp-tables.
And setting a logical variable when a file was converted so the directory is scanned again.
This works great now.
The only reason for using import from os-dir in a repeat loop was cause me and my colleagues were in the impression that it would work more dynamically. Apparently this is not the case. According to the Progress Helpdesk Import from OS-DIR scans the directory once and then walks through the files it found then.
They also told me that it should stop after the last file and not process newly created files. After I finally got them to see that this behaviour is not constant they told me it would be reported as bug for windows 98. (As far as I could tell it also happens with windows 2000, even with progress 9.1c).

Thnx again all,

P.S.
For anyone that is interested this is the code I sent to Progress that finally showed them the problem.
When running it pointing to a directory with one file it creates 1 new file and then stops.
When running it again pointing to the same directory (now 2 files), it keeps on running (endless loop, or till a random name is generated that already exists? i assume).
According to Progress it should stop after the 2 files that were in the directory, however for me it keeps going on win98 and win2000 with versions 8.3b and 9.1c.
They also mentioned that it could be due to regional settings or installed windows language version. Anybody got any thoughts on that?

DEFINE VARIABLE cImportDir AS CHARACTER NO-UNDO.
DEFINE VARIABLE c1 AS CHARACTER NO-UNDO. /*file name*/
DEFINE VARIABLE c2 AS CHARACTER NO-UNDO. /*file name with path*/
DEFINE VARIABLE c3 AS CHARACTER NO-UNDO. /*file type*/
DEFINE VARIABLE c4 AS CHARACTER NO-UNDO.

UPDATE cImportDir FORMAT "x(30)" LABEL "Directory" WITH SIDE-LABELS 1 COLUMN.
INPUT FROM OS-DIR(cImportDir).
REPEAT:
IMPORT c1 c2 c3.
IF c1 = "." OR c1 = ".." THEN NEXT.
IF INDEX(c3, "D":U) = 1 THEN NEXT.
IF INDEX(c3, "F":U) > 0 THEN Do:
ASSIGN c4 = cImportDir + "\":U + STRING(RANDOM(400, 5000)) + ".txt":U.
OS-COPY VALUE(c2) VALUE(c4).
END.
MESSAGE c2 VIEW-AS ALERT-BOX.
END.
INPUT CLOSE.
 
Top