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

Get all Files in Directory (including Subdirectories) 2

Status
Not open for further replies.

craigsboyd

IS-IT--Management
Joined
Nov 9, 2002
Messages
2,839
Location
US
I wrote this code in response to a question in thread184-671687 It 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. 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)
*!* EXTERNAL ARRAY aryAllFiles
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


Slighthaze = NULL

[ul][li]FAQ184-2483
An excellent guide to getting a fast and accurate response to your questions in this forum.[/li][/ul]
 
Hi SLighthaze,
I undesratnd why you started it separately as a thread. Star for you. And also, I am providing the alternative, so users can have choice..

See also my FAQ
A cursor to collect all files in a specific directory
FAQ184-3102
This can be made to collect all recursive also...
We can make that as a function..
*************************************************
=doDirFiles(cmyDir,.t.) for recursive
BROW
=doDirFiles(cmyDir,.f.) for root files only
BROW
*********************************************************
** Author : Ramani (Subramanian.G)
** FoxAcc Software / Winners Software
** ramani_g@yahoo.com
** Type : Freeware with reservation to Copyrights
** Warranty : Nothing implied or explicit
** Last modified : 31 January, 2003
*********************************************************
** The following uses Filer.DLL and
** extracts all files in a directory as a cursor.
*********************************************************
FUNCTION doDirFiles
PARAMETERS tcDir, tlRecurse

IF VARTYPE(tcDir) # "C"
tcDir = "C:\"
ENDIF
IF VARTYPE(tlRecurse) = "U"
tlRecurse = .t.
ENDIF

CREATE CURSOR filename (cfilename c(128))
omyfiler = CREATEOBJECT('Filer.FileUtil')
omyfiler.searchpath = ADDBS(ALLT(tcDir))

IF tlRecurse
omyfiler.subfolder = 1 && Recurse
ELSE
omyfiler.subfolder = 0 && Only roo directory
ENDIF
oMyFiler.SortBy = 0
omyfiler.FIND(0)
LOCAL ncount
ncount = 1
FOR nfilecount = 1 TO omyfiler.FILES.COUNT
IF omyfiler.FILES.ITEM(nfilecount).NAME = "." OR ;
omyfiler.FILES.ITEM(nfilecount).NAME = ".."
LOOP
ENDIF
APPEND BLANK
REPLACE cfilename ;
WITH UPPER(omyfiler.FILES.ITEM(nfilecount).PATH)+ ;
UPPER(omyfiler.FILES.ITEM(nfilecount).NAME)
ENDFOR
RETURN
**********************************************************

:-)

ramani :-)
(Subramanian.G)
 
ramani,

Yes, a new thread since our responses were to a thread titled "copy file" and not all members would look under that. Your solution is a good one as well (star), Filer.dll is very useful. For members not familiar with Filer.dll here is a link to some information...


Slighthaze = NULL

[ul][li]FAQ184-2483
An excellent guide to getting a fast and accurate response to your questions in this forum.[/li][/ul]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top