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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

FileSeek() sorting by filename

Status
Not open for further replies.

SM777

Technical User
Mar 7, 2001
208
GB
I have a routine which scans a directory looking for TXT files ready for parsing.

How can I ensure that the files are read in a certain sequence?

If the filenames are generated sequentially:

file1.txt 10:14 21-May
file2.txt 10:17 21-May
file3.txt 10:20 21-May

Then they are read in the correct order.

However, If one of the files is written before the others:

file2.txt 10:17 21-May
file1.txt 10:14 21-May
file3.txt 10:20 21-May

Then fileseek() will not read file2 before file1 and this causes problems.

How can I ensure that the files are all read sequentially from 1 to 3? Is this just a matter of loading the file directory into an array and sorting? Or is there an option in fileseek to presort the dir before scanning it?



 
Use the DIRECTORY() or ADIR() commands to get a list of matching files. Sort the arrays and work your way through the resulting arrays that will then be in the correct order.

Ian Boys
DTE Systems Ltd
 
Bozz,

You're on point with your suggestion. I took it a step further which makes things a lot easier. I actually use a julian date in my file naming convention (i.e. FIL04143.txt) then use a wildcard with ADIR()

Example:

p_path := "FIL*.TXT"
DECLARE a_file_ary[ADIR(p_path)]
ADir(p_path, a_file_ary)
a_filesort := Asort(a_file_ary,,,{|x,y| x>y}) // sort in descending order
.........
........

This assures LIFO when printing the most recent file (I mainly use this routine in report generation/printing)

FBM

"If you want SQUARE work, you DON'T cut CORNERS!!!" ... :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top