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

Find a folder

Status
Not open for further replies.

paul1941

Programmer
May 25, 2006
32
BE
Hello all,

How can I know if a folder with the name 'Movies' exists on an users computer from within vfp9?

regards

Paul
 
Try:
Code:
CLEAR
? IsFolderExists([C:],[finger_com])


FUNCTION IsFolderExists(lcWhereToSearch, lcFolderSearched)
 LOCAL ret_val, lnFor
 LOCAL lnFolders, laFolders[1]
 lcWhereToSearch = ADDBS(lcWhereToSearch)
 ret_val = []
 IF DIRECTORY(lcWhereToSearch+lcFolderSearched)
    ret_val = lcWhereToSearch+lcFolderSearched
 ELSE
    lnFolders = ADIR(laFolders,ADDBS(lcWhereToSearch)+[*.*],[D])
    FOR lnFor = 1 TO lnFolders
        IF NOT LEFT(laFolders[lnFor,1],1) == [.] AND;
          [D] $ laFolders[lnFor,5]
          ret_val = IsFolderExists(lcWhereToSearch+laFolders[lnFor,1], lcFolderSearched)
          IF NOT EMPTY(ret_val)
             EXIT
          ENDIF
        ENDIF
     NEXT
  ENDIF

RETURN ret_val

Borislav Borissov
VFP9 SP1, SQL Server 2000/2005.
MVP VFP
 
Paul:
I have just written a recursive function to fill an array with all subfolders of a given path (e.g. C:\). The initial ideas came from the FoxWiki forum site (
Code:
* Remove *~
*	to use this code!
* Recurse.prg, DW 12/20/2006, based on ideas from FoxWiki forum
* Include the code below to call the function, replacing lcPath with x:\path, which should be
*	a full path to the folder to recurse
* Declare the array to fill with the full tree of folder names.  Hide the array from upper
*	level programs
*~PRIVATE lnRetVal
*~IF TYPE("laDirListing") # "U"
*~	DIMENSION laDirListing[1]
*~ELSE
*~	PRIVATE laDirListing
*~	DIMENSION laDirListing[1]
*~ENDIF
* Call the recurse function with a full path and a True
*~lnRetVal = Recurse("SOME NICE PATH", .T.)
* When function has finished, you can look at the array and do stuff with it
*~IF lnRetVal > 0
*~* Sort by folder name column, all rows, descending order, case insensitive
*~* This way, we process the deepest folders first.
*~		=ASORT(laDirListing,1,-1,1,0)
*~	FOR i = 1 TO ALEN(laDirListing)
*~*	DO STUFF WITH THE FOLDER NAMES HERE.
*~*	SPACES ARE PRESERVED IN THE FOLDER NAMES FOR YOUR ENJOYMENT!
*~		?laDirListing[i]
*~	ENDFOR
*~ENDIF
*~* Only the local, private copy of the variables are released!
*~RELEASE laDirListing, lnRetVal
****************************************
FUNCTION Recurse
	LPARAMETERS lcFullPathDir, lbInit
	LOCAL lnPtr, lnFileCount, lcDir, lnRetVal
	LOCAL ARRAY laFileList[1,5]

* Trim off any trailing backslash
	lcFullPathDir = ALLTRIM(lcFullPathDir)
	IF RIGHT(lcFullPathDir,1) == "\"
		lcFullPathDir = LEFT(lcFullPathDir, LEN(lcFullPathDir) - 1)
	ENDIF
* Read the chosen directory.
	lnFileCount = ADIR(laFileList, lcFullPathDir + '\*.*', 'D')
* If there are files, sort the array so directories are listed first
	IF lnFileCount = 0
		RETURN 0
	ENDIF
	=ASORT(laFileList,5)
	lnDirs = 0
	FOR lnPtr = 1 TO lnFileCount
		IF 'D' $ laFileList[lnPtr, 5]
* Get directory name.
			lcDir = laFileList[lnPtr, 1]
* Ignore current and parent directory pointers.
			IF lcDir != '.' AND lcDir != '..'
* Move the first child level folder names into the permanent array
				lnDirs = lnDirs + 1
				IF !EMPTY(laDirListing[1])
					DIMENSION laDirListing[ALEN(laDirListing)+1]
				ENDIF
				laDirListing[ALEN(laDirListing)] = lcFullPathDir + "\" + ALLTRIM(laFileList[lnPtr,1])
			ENDIF
		ENDIF
	ENDFOR
	IF lnDirs = 0
* If there are no folders, return now.
		RETURN 0
	ENDIF
* We have some folders in the first directory we wanted and they are in the permanent array.
* Now traverse the permanent array, looking for subfolders.
	IF lbInit
		j = ALEN(laDirListing)
		lnTempi = 1
		DO WHILE lnTempi <= j
			lnRetVal = Recurse(laDirListing[lnTempi], .F.)
			j = ALEN(laDirListing)
			lnTempi = lnTempi + 1
		ENDDO
	ENDIF
	RETURN ALEN(laDirListing)
ENDFUNC
****************************************

For instance, in your application, you can fill the array laDirListing then scan through it looking for:
"MOVIES" $ UPPER(laDirListing[scancount])

Hope this helps!
Dan

Dan Walter
Daniel.Walter@uvm.edu
try { succeed(); } catch(E) { tryAgain(); } finally { rtfm(); }
 
Hello Boris,

I used your code to find the folder "Movies". It was working fine.
But MOVIES can be a folder or a subfolder in more than one folder
Your code returns the first time MOVIES is found, I need to have a list with al the folders containing MOVIES.
How can I manage it?

regards,

Paul
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top