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

Search Folders & SubFolders for Files

Usefull Functions & Procedures

Search Folders & SubFolders for Files

by  GriffMG  Posted    (Edited  )
Someone asked how to do this in a question recently, I knocked up some very scruffy code to do it for them - not using a run command.

Then I polished it up a bit, so here is the finished code - supplied as a function:

Code:
FUNCTION SUBFOLDERSEARCH
	PARAMETERS m.TOPLEVEL,m.MASK,m.TEMPDIR,m.PROGRESS,m.TEMPFILE,m.TMPDIRS,m.LEAVEOPEN
	PRIVATE m.TOPLEVEL,m.MASK,m.TEMPDIR,m.TEMPFILE,m.TMPDIRS,m.LEAVEOPEN,m.PROGRESS,I,X,m.LASTPATH,NUMFILES,NUMDIR
	PRIVATE m.OLDAREA
	DIMENSION ALIST[1,5],FLIST[1,5]
	IF PCOUNT() < 7
		** This parameter controls whether the resultant table of file names is left as the current cursor
		M.LEAVEOPEN = .F.
	ENDIF
	IF PCOUNT() < 6
		** This one controls the name of the temporary table used to track folders not yet processed
		M.TMPDIRS = "TMPDIRS"
	ENDIF
	IF PCOUNT() < 5
		** This one allows the user to specify the name of the table that will hold any filenames located
		M.TEMPFILE = "TMPFILES"
	ENDIF
	IF PCOUNT() < 4
		** The fourth parameter controls the use of a wait window to show that something is happening
		M.PROGRESS = .F.
	ENDIF
	IF PCOUNT() < 3
		** The third parameter controls the name of the folder to be used for the temporary folder
		M.TEMPDIR = "C:\TEMP\"
	ENDIF
	IF PCOUNT() < 2
		** The second parameter is the pattern to be searched for
		M.MASK = "*.*"
	ENDIF
	IF PCOUNT() < 1
		** The first parameter is used to specify where the search should start
		M.TOPLEVEL = ".\"
	ENDIF
	M.OLDAREA = SELECT()
	** CREATE A COUPLE OF TEMPORARY TABLES - ONE FOR THE FINAL FILENAME LIST (COMPLETE WITH PATHS
	SELECT 0
	CREATE TABLE (m.TEMPDIR+M.TEMPFILE) FREE (FULLNAME C(240) NOT NULL, FILESIZE N(12,0) NOT NULL, FILEDATE D NOT NULL, FILETIME C(8) NOT NULL, FILEATTR C(20) NOT NULL)
	USE (m.TEMPDIR+M.TEMPFILE) ALIAS TMPFILES EXCLUSIVE
	INDEX ON FULLNAME TAG TMPFILES
	** AND ONE FOR THE FOLDERS TO SEARCH
	SELECT 0
	CREATE TABLE (m.TEMPDIR+M.TMPDIRS) FREE (FPATH C(240) NOT NULL,PROCESSED C(1) NOT NULL)
	USE (m.TEMPDIR+M.TMPDIRS) ALIAS TMPDIRS EXCLUSIVE
	** USE AN INDEX TO KEEP CHECKING FOR UNPROCESSED SUBFOLDERS
	INDEX ON PROCESSED TAG TMPDIRS

	** START BY PUTTING THE 'WHERE TO START' IN LIST OF UNPROCESSED FOLDERS
	SELECT TMPDIRS
	SET ORDER TO TMPDIRS
	APPEND BLANK
	REPLACE FPATH WITH m.TOPLEVEL
	REPLACE PROCESSED WITH "N"
	GO TOP

	SELECT TMPDIRS
	SET ORDER TO TMPDIRS
	SEEK "N"
	** SCAN THE LIST OF SUBFOLDERS - LOOKING FOR UNPROCESSED ONES
	DO WHILE .NOT. EOF()
		** MAKE A NOTE OF THE FOLDERS PATH
		M.LASTPATH = ALLTRIM(FPATH)+IIF(RIGHT(ALLTRIM(FPATH),1)<> "\","\","")
		WAIT "Checking..." + m.LASTPATH WINDOW NOWAIT TIMEOUT 1
		** CHECK FOR FILES MATCHING MASK
		NUMFILES = ADIR(FLIST,m.LASTPATH+m.MASK)
		FOR I = 1 TO NUMFILES
			** IGNORE THE . AND .. ONES
			IF FLIST[i,1] <> "." .AND. FLIST[i,1] <> ".."
				** STICK THE FILES IN THE FILE LIST
				SELECT TMPFILES
				APPEND BLANK
				*REPLACE FPATH 	 WITH M.LASTPATH
				REPLACE FULLNAME WITH m.LASTPATH+FLIST[i,1]
				REPLACE FILESIZE WITH FLIST[i,2]
				REPLACE FILEDATE WITH FLIST[i,3]
				REPLACE FILETIME WITH FLIST[i,4]
				REPLACE FILEATTR WITH FLIST[i,5]
			ENDIF
		NEXT
		** MARK THAT FOLDER AS PROCESSED
		SELECT TMPDIRS
		REPLACE PROCESSED WITH "Y"
		** NOW CHECK THE FOLDER FOR SUBFOLDERS - HAVE TO DO THIS BECAUSE THE MASK
		** FOR FILES OF INTEREST WILL BE DIFFERENT TO THE ONE FOR FOLDERS
		NUMDIR = ADIR(ALIST,m.LASTPATH+"*.*","D")
		** FOR EACH FOLDER FOUND
		FOR I = 1 TO NUMDIR
			** IGNORE THE ONES THAT ARE NOT FOLDERS AND THAT ARE CALLED . OR ..
			IF "D"$ALIST[i,5] .AND. ALIST[i,1] <> "." .AND. ALIST[i,1] <> ".."
				SELECT TMPDIRS
				APPEND BLANK
				REPLACE FPATH WITH m.LASTPATH+ALIST[i,1]
				REPLACE PROCESSED WITH "N"
			ENDIF
		NEXT
		** LOOK FOR THE NEXT UNPROCESSED FOLDER
		WAIT CLEAR
		SELECT TMPDIRS
		SET ORDER TO TMPDIRS
		SEEK "N"
	ENDDO
	RELEASE FLIST
	RELEASE ALIST
	SELECT TMPDIRS
	USE
	SELECT TMPFILES
	IF !M.LEAVEOPEN
		USE
		SELECT(m.OLDAREA)
	ENDIF
	RETURN(m.TEMPDIR+m.TEMPFILE+IIF(UPPER(RIGHT(M.TEMPFILE,4)) <> ".DBF",".DBF",""))

It takes up to seven parameters - which SHOULD be evident from the code!

The function returns the name and location of the resultant table of filenames

To call the function you do something like this:
Code:
    myTableName = SearchSubFolders("D:\","*.DBC","C:\TEMP\",.T.)

or

Code:
    myTableName = SearchSubFolders("D:\","*.DBC","C:\TEMP\",.T.,"TMPFILES","TMPDIRS",.F.)
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