|
Technical answers from the trenches |
|
FileStream: Recursively Searching Directories
| ||||
by Lance Leonard |
Posted: 21 July 2000 |
|||
  |
Applies to: All Versions |
|||
  |
Audience: Beginner |
|||
Question: How can I use FileSystem to recursively search directories?Answer: One way is to use an array as a "stack" that stores directories to be searched, as shown in the following code sample:
astrPaths.addLast( getAliasPath( ":WORK:" ) )
ignoreCaseInStringCompares( Yes ) ; important for NT
while loRepeat
siNumDirs = siNumDirs + 1 ; increment dir count
; Get a list of the files in the working directory,
; including ".", "..", and subdirectories.
fs.findFirst( astrPaths[ 1 ] + "\\*.*" )
fs.enumFileList( astrPaths[ 1 ] + "\\*.*", astrFiles )
; For each file, check its type and then react accordingly.
for siCounter from 1 to astrFiles.size()
strFileNm = astrFiles[ siCounter ]
; Skip this file and the standard subdirectory
; indicators.
If ( upper( strFileNm ) = SELFNAME ) OR
( strFileNm = "." ) OR ( strFileNm = ".." ) then
loop
endIf
splitFullFileName(
Upper( astrPaths[ 1 ] + "\\" + strFileNm ), dynPieces )
setMouseShape( mouseWait )
switch
case isDir( astrPaths[ 1 ] + "\\" + strFileNm ) :
; If the file is a directory, then "push" it onto the
; the array, which acts like a stack.
astrPaths.addLast( astrPaths[ 1 ] + "\\" + strFileNm )
; other CASE branches here as needed for your other
; processing.
endSwitch
setMouseShape( mouseArrow )
endFor
astrPaths.remove( 1, 1 ) ; "pop" the current dir.
; if we haven't already set this to FALSE, then we need
; to see if there are other directories to process.
if loRepeat then
loRepeat = ( astrPaths.size() > 0 )
endIf
endWhile
How it works:The above code uses the astrPaths array as a source of directories to be searched. Initially, this array is populated with the working directory. The loRepeat flag controlling the outer loop (WHILE) is true when astrPaths contains directories to be searched. The inner loop (FOR) checks to see if each file (see the first evaluation in the SWITCH statement) is a directory. If so, then that name is added to the bottom of the astrPaths array. When all files in a directory have been processed, the first item in the astrPaths array is removed. Also, loRepeat is reset to reflect the number of directories to search, provided it hasn't already been set to FALSE elsewhere in the script. Possible UsesThis is a powerful technique, one that can help with a variety of tasks. For example, you can:
You're really limited only by your imagination. Keep in mind, however, that the sample script is limited to verifying the stylesheets for forms in the current directory and below. (If you're curious why this is important, please see Avoiding Style Sheet Problems.) We leave it as an exercise for the reader to extend it to reports and other documents. Warning: There have been reports of resource leaks in Paradox 9's handling of FileSystem. These have been reported to Corel and may be addressed in a future version or service pack. (To date, these reports indicate that the problem can be still seen after applying Service Pack #3.) Because of this, you may wish to limit your use of this technique to utility scripts and infrequently-run processes. |
|||
|
||||||||
|
Copyright © 2000-2004, techtricks.com; All Rights Reserved. Acknowledgements, Disclaimers, Terms and Conditions. |
||||||||
|
Article last updated on 31 May 2003
|
||
|
|
||
|
[- End -]
|