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!

Dirs and files 5

Status
Not open for further replies.

ShyFoxBack

Programmer
Joined
Feb 24, 2004
Messages
18
Location
RO
Hy,
I want an ideea. I have to list all the dirs and subdirs and the files from a volume in a table with full paths. I use adir() function but it's hard. I would like to know if I can do that with a some api functions or if there are some other functions in foxpro 7 or 8 that can do a recursive search through all the dirs and subdirs.
Thanks
 


Take a look at faq184-3096 or this example (Please note that this will list ALL folders on your computer, it may take a few minutes)
Description
Returns a list of all the folders on a computer. This can take 15 minutes or more to complete, depending on the number of folders on the computer.

Script Code

Code:
strComputer = "."
objWMIService = GetObject("winmgmts:" +"{impersonationLevel=impersonate}!\\" + strComputer + "\root\cimv2")
colFolders = objWMIService.ExecQuery("Select * from Win32_Directory")
For Each objFolder in colFolders
  ?objFolder.Name
Next





Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483 first.
 
Here's a FAQ I wrote on it (it creates an array, but you could certainly make a cursor/table with a tiny bit of modification). It uses Adir() and recursion to make it not so difficult.

Get Files in Directory (including subdirectories)
faq184-4256

boyd.gif

craig1442@mchsi.com
"Whom computers would destroy, they must first drive mad." - Anon​
 
craigsboyd (IS/IT--Manageme)
Looking at your code and trying to find a *.mp3 through a tree of subdirectories you'll find that your program will fail.
Sorry for not telling you earlier but I was so busy. Now I tried your e.g. at last and I found this.
Regards
 
Will fail? Could you provide more information as to the exception you are encountering and perhaps whether you have found it related to the mp3 files, subdirectory structure, or anything else? This would allow me to test it, reproduce the error, and fix it at this end. Thanks.

boyd.gif

craig1442@mchsi.com
"Whom computers would destroy, they must first drive mad." - Anon​
 
craigsboyd (IS/IT--Manageme)

Your program will look for the ocurence of the *.mp3 files (or other extension) in the root directory. After that will stop searching. Why?
I have a guess: your function looks for a *.* pattern and it will always find in it's way all the possible directories in the root dir. But if you put a *.mp3 pattern (or other) you will never find a file.mp3 with the 'D' attribute, because it's not possible so it will not fire itself again.
(Mp3 Files was just an example but you can change
the *.* in the

=[blue]ADIR[/blue](aryTemp, "*.*","AHRSD",1)

with

=[blue]ADIR[/blue](aryTemp, "*.[blue]mp3[/blue]","AHRSD",1)

and you'll never go into the subs of the root because they will never be detected)
I think that after you get the results in your array you must filter them by the extenssion you're looking for and give the expected results in another array to the enduser. So, I think, you're function is not complet, yet. But you're work is apreciated an it was a real help for me.

Thank you.
Regards
 
ShyFoxBack,

Yes, I believe you are right. Thanks for the heads up on this and when time permits I will make the appropriate changes...perhaps use sys(2000) in conjunction with the ADIR() would provide for better performance.

boyd.gif

craig1442@mchsi.com
"Whom computers would destroy, they must first drive mad." - Anon​
 
Hi ShyFox
Probably this could help you..


A cursor to collect all files in a specific directory
faq184-3102

:-)

____________________________________________
ramani - (Subramanian.G) :-)
 
ramani (Programmer)
I want a cursor to colect a specific pattern of file in a cursor or array. So, if you give me a *.mp3 skeleton and path 'c:\media' I must return all the mp3 files in that path.
 
ShyFoxBack

Here a bit of code that will do two level (root and one level down), it would need to be completed to do all levels, but its a start.
Code:
Local lcExt
Create Cursor filenames (filename c(128))
TargetFolder = "C:\MEDIA"
loShell = Createobject("Shell.Application")
loFolder = loShell.Namespace(TargetFolder)
colItems = loFolder.Items
For i = 1 To colItems.Count-1
	If colItems.Item(i).Type = "File Folder"
		subdir(TargetFolder+"\"+colItems.Item(i).Name)
	Endif
	lcExt = Justext(colItems.Item(i).Name)
	If lcExt = "MP3"
		Insert Into filenames (filename) Values (TargetFolder+"\"+ALLTRIM(colItems.Item(i).Name))
	Endif
Next
Browse Nowa
Procedure subdir(cTarget)
loFolder = loShell.Namespace(cTarget)
colItems2 = loFolder.Items
For Y = 1 To colItems2.Count-1
	lcExt = Justext(colItems2.Item(y).Name)
	If lcExt = "MP3"
		Insert Into filenames (filename) Values (cTarget+"\"+ALLTRIM(colItems2.Item(y).Name))
	Endif
Next

Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483 first.
 
mgagnon (Programmer)
Indeed it's a start. I shall try your code.
Regards
 
Hi

Again to repeat the same FAQ I mentioned..
A cursor to collect all files in a specific directory
FAQ184-3102


I just put the code below to illustrate..

*****************************************
CREATE CURSOR filename (cfilename c(128))
omyfiler = CREATEOBJECT('Filer.FileUtil')
omyfiler.searchpath = 'C:\WINDOWS' && Search Directory
omyfiler.subfolder = 1 && 1=add all subdirectories else 0
oMyFiler.FileExpression = "*.DLL" && search expression
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
BROW
**********************************************************

Now change ..
omyfiler.searchpath = 'C:\MEDIA' && Search Directory
oMyFiler.FileExpression = "*.MP3" && search expression
you will get what you want.

omyfiler.subfolder = 0 && to get no sub directories..

oMyFiler.FileExpression = "*.MP3;*.WAV" && search expression to get both MP3 and WAV files.

:-)


____________________________________________
ramani - (Subramanian.G) :-)
 
Thank you for all of your answers.
Regards
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top