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

Get Files in Directory (including subdirectories)

Usefull Functions & Procedures

Get Files in Directory (including subdirectories)

by  craigsboyd  Posted    (Edited  )
Slighthaze = [color blue]NULL[/color]

[img http://www.sweetpotatosoftware.com/ttimages/subdirs.gif]

The following code will fill up the array aryFiles with a list of all the files (fullpath) in the selected directory (including subdirectories). Besides being useful it is a good example of using recursion. No external DLLs or COM objects are required, this solution is pure VFP. The SET STEP ON line is so that you can easily see the resultant array in the debugger's LOCALS window. Cut-n-paste the code below into a prg file and run it from within VFP to see how it works.

Dimension aryFiles(1)
=GetAllFiles(GETDIR(), @aryFiles)
SET STEP ON

FUNCTION GetAllFiles(cDirectory, aryParam)
LOCAL ARRAY aryTemp(1,5)
LOCAL nCount, nMax, nLen, cFile
SET DEFAULT TO (cDirectory)
=ADIR(aryTemp, "*.*","AHRSD",1)
nMax = ALEN(aryTemp,1)
FOR nCount = 1 TO nMax
cFile = ALLTRIM(aryTemp(nCount,1))
IF !(cFile == ".") AND !(cFile == "..")
IF "D" $ aryTemp(nCount,5)
=GetAllFiles(ADDBS(cDirectory + cFile), @aryParam)
ELSE
nLen = ALEN(aryParam)
IF !EMPTY(aryParam(nLen))
DIMENSION aryParam(nLen + 1)
nLen = nLen + 1
ENDIF
aryParam(nLen) = cDirectory + cFile
ENDIF
ENDIF
ENDFOR
ENDFUNC
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top