find ALL files in folder and sub folders...how?

Byak@

Active Member
Licensed User
I thought much but and have not found a beautiful way...
how can i find ALL files in given dir?include subdirs
 

Byak@

Active Member
Licensed User
Greating Flippo)i'm work with soirce of your explorer but not find,how can i do it.you find files in subfolder when click it.but i must find all files at once
 

tsteward

Well-Known Member
Licensed User
Longtime User
Man get out of my head.
I just asked the same question in a different way.

Anyway looking forward to the solution as I also need to do this!!!
 

Cableguy

Expert
Licensed User
Longtime User
FROM BASIC4PPC HELP

DirSearch

--------------------------------------------------------------------------------

Adds all subdirectories matching the search pattern in the specified path to an ArrayList and returns the number found.
Syntax: DirSearch (ArrayList, Path [,Search Pattern])
If the Search Pattern is omitted DirSearch will return all the subdirectories in the directory.
Search Pattern can include '*' and '?'.
* - Finds zero or more characters.
? - Finds exactly one character.
Example: (alDir is an ArrayList control)
FileSearch (alDir, "\My Documents")


This example will add all the subdirectories in My Documents folder (in the device) to the ArrayList alDir.
------------------------------------------------------
FileSearch

--------------------------------------------------------------------------------

Adds all files matching the search pattern in the specified path to an ArrayList and returns the number of files found.
Syntax: FileSearch (ArrayList, Path [,Search Pattern])
If the Search Pattern is omitted FileSearch will return all the files in the directory.
Search Pattern can include '*' and '?'.
* - Finds zero or more characters.
? - Finds exactly on character.
Example: (alFiles is an ArrayList control)
FileSearch (alFiles, "\My Documents", "*.txt")


This example will add all the files with txt extension in My Documents folder (in the device) to the ArrayList alFiles.
 

Byak@

Active Member
Licensed User
Cableguy
we know how find files and find folders.but how find files in all subfolders and subsubfolders and sub...subfolders?
 

Cableguy

Expert
Licensed User
Longtime User
use a bit of logic...
if root folder has subdir, then search each subdir for folders...Until the result is 0Then you will have a series of array with ALL THE SUBFOLDERS OF YOUR MAIN ROOT

Same with the files...

Example:
IF DirSearch(Root(),"\MyDocuments")>0 Then
For x=0 to arraylen(root())-1
Textbox1.text=TextBox1.text&Root(x)&crlf 'this will populate a textbox with the root values
Next
For x=0 to Arraylen(Root())-1
DirSearch(Control(Level(),Root(x))
Textbox1.text=TextBox1.text&Root(x)&crlf
Next
End If

NOTE this is untested code, root and level are arraylist controls...
 

Byak@

Active Member
Licensed User
Cableguy you read my post?i must find ALL FILES in folder,sub-folders,subsub-folders,subsubsub-folders,subsubsub-folders...subsubsubsubsubsubsubsubsubsubsubsub-folders.
yes,you may use 1000 cicles and create 1000 arraylists...but i want a beautiful way...

p.s. in your code your have some errors)and your cade show only folders from root
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Here:
B4X:
Sub Globals
    'Declare the global variables here.

End Sub

Sub App_Start
    Form1.Show
    FindAllFiles("\My Documents")
    For i = 0 To alFiles.Count-1
        listbox1.Add(FileName(alFiles.Item(i))) 'Remove FileName if you need the full 
    Next
End Sub

Sub FindAllFiles(root)
    alFolders.Clear 'ArrayList
    alFiles.Clear 'ArrayList
    alFolders.Add(root)
    i = 0
    Do While alFolders.Count > i
        'search one folder
        currentFolder = alFolders.Item(i)
        DirSearch(alFolders,currentFolder) 'Add all subfolders in this folder to the list.
        FileSearch(alFiles,currentFolder,"*.*") 'Add all files from this folder.
        b = alFiles.Count
        i = i + 1
    Loop
End Sub
 

Byak@

Active Member
Licensed User
Erel big-big-big thanks!!!i'm don't know what dirsearch add folders to arraylist,i'm think it replace)
big thanks!
 

tsteward

Well-Known Member
Licensed User
Longtime User
Thanks erol,
The logic was eluding me and as I was reading down this thread I started thinking I needed a do while loop rather than If's

Then I came to your post. It must frustrate the more experienced users sometimes but we can't always see the forrest when the trees get in the way.

Thanks for your help
 

joel2009

Member
Licensed User
I'm getting an error everytime with "end sub" on the findallfiles sub.... and without it i get an argument out of range exception. Anyone know what i'm doing wrong?

B4X:
Sub App_Start
    Form1.Show
    FindAllFiles("\Storage Card\Program Files")
    For i = 0 To alFiles.Count-1
        listbox1.Add(FileName(alFiles.Item(i))) 'Remove FileName if you need the full 
    Next
End Sub

Sub FindAllFiles(root)
    alFolders.Clear 'ArrayList
    alFiles.Clear 'ArrayList
    alFolders.Add(root)
    i = 0
    Do While alFolders.Count > i
        'search one folder
        currentFolder = alFolders.Item(i)
        DirSearch(alFolders,currentFolder) 'Add all subfolders in this folder to the list.
        FileSearch(alFiles,currentFolder,"*.exe*") 'Add all files from this folder.
        b = alFiles.Count
        i = i + 1
    Loop

I'm trying to write a program that will restore all of my shortcuts to my start menu after flashing.

EDIT: from what i can tell... the out of range exception has something to do with arrays.
 

joel2009

Member
Licensed User
It doesn't run with the end Sub. (it gives an error and closes on me)
 

Attachments

  • program.sbp
    998 bytes · Views: 291

joel2009

Member
Licensed User
B4X:
error compiling program.
error descrition: Variable main.b in sub main.findallfiles is never used.
error occurred on line: 26
End Sub
 

Cableguy

Expert
Licensed User
Longtime User
Joel, you can safely remove the
B4X:
b = alFiles.Count
Code line from the code, and the error will no longer be trigged, or simply un-check the "check un-used variable" option...
 

joel2009

Member
Licensed User
Joel, you can safely remove the
B4X:
b = alFiles.Count
Code line from the code, and the error will no longer be trigged, or simply un-check the "check un-used variable" option...
Thanks much, cant believe i didnt see that haha....
 
Top