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!

How to read a directory and his subdirectories 1

Status
Not open for further replies.

rogerbl

Programmer
Apr 22, 2008
2
ES
I've to do a function that search a specific files in a tree of directories.

I've then to read the tree and look in each directory for a file pattern.

I see the function dirlist but it's not useful because it only search on the current directory.

How I could read the tree directory?

Sorry about my bad english.
 
It's called recursion... You'll have to loop through each item from the dirlist, and test it to see if it's a folder. If it is, you'll have to call the function over again, passing the folder in.

The dirlist isn't always helpful, unless it's used from a window function, because it must be tied to a listbox. I personally use OLEObjects for my file structure handling...
 
Thanks, I'm trying something with recursion but unfortunatelly I didn't find any solution.

Has PB9 something similar like FileSystemObject of Visual basic ?
 
I can give you a sample script for a function to retrieve a list of all files (paths) using OLEobjects...

Basically, to start off with you'll need to create a structure called nvo_file_list. It has one item, a string array named as_file[].

The following is the code from the function... Let me know if you have any questions, or want the full pbl.
Long ll_cnt, ll_temp
nvo_file_list lstr_parms, lstr_temp
OLEObject obj_shell, obj_folder, obj_items, obj_item

IF DirectoryExists( as_folder ) THEN
//initiate the shell object
obj_shell = CREATE OLEObject
obj_shell.ConnectToNewObject( 'shell.application' )

IF IsValid( obj_shell ) THEN
//assign folder object
obj_folder = obj_shell.NameSpace( as_folder )

IF IsValid( obj_folder ) THEN
//assign objects from that folder
obj_items = obj_folder.Items

IF IsValid( obj_items ) THEN
//loop through items (zero-indexed)
FOR ll_cnt = 0 TO obj_items.Count
//assign individual item
obj_item = obj_items.Item( ll_cnt )

IF IsValid( obj_item ) THEN
//check to see if item is a folder
IF obj_item.IsFolder THEN
//Yup, it's a recursive function!
lstr_temp = uf_file_list( obj_item.Path )

//loop through new files
FOR ll_temp = 1 TO UpperBound( lstr_temp.as_file )
//add to list
lstr_parms.as_file[ UpperBound( lstr_parms.as_file ) + 1 ] = lstr_temp.as_file[ ll_temp ]
NEXT

ELSE
//add to list
lstr_parms.as_file[ UpperBound( lstr_parms.as_file ) + 1 ] = obj_item.Path

END IF
END IF
NEXT
END IF
END IF
END IF
END IF

//clear memory
DESTROY obj_shell
DESTROY obj_folder
DESTROY obj_items
DESTROY obj_item

//return list
RETURN lstr_parms

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top