B4J Library Walk File Tree: Recursive file find

This library has one class which finds files that match the specified glob pattern in any selected path, then they are returned in a list.
It was adopted from Oracle Java Tutorials.
It uses the powerful Files method "walkFileTree" and "FileVisitor" class to achieve this goal.
The files or directories that match the pattern and the number of matches are printed.
If no match is found, the list will be empty, and number of matches is zero.
Download the *.jar and *.xml files and put them in the additional libraries folder. when refreshed, the "Libraries"
list will show findFiles . Choose it. The class name will be 'walkFileTree'.
Usage:
B4X:
     Dim ff As walkFileTree
    'You must initialize the class
    ff.Initialize
    'Create a list to contain the results
    Dim foundFiles As List
    foundFiles.Initialize
    'Do the work
    foundFiles= ff.search(rootPath,searchPattern)
    If foundFiles.Size > 0 Then
           For i = 0 To  foundFiles.Size -1
            log(foundFiles.Get(i))
           Next
     End If
The included example also lists the system accessible hard drives in a combobox.
 

Attachments

  • findFiles.zip
    4.4 KB · Views: 47
  • findfileEx.zip
    2.8 KB · Views: 49
Last edited:

jkhazraji

Active Member
Licensed User
Longtime User

What Is a Glob?​

You can use glob syntax to specify pattern-matching behavior.
A glob pattern is specified as a string and is matched against other strings, such as directory or file names. Glob syntax follows several simple rules:
  • An asterisk, *, matches any number of characters (including none).
  • Two asterisks, **, works like * but crosses directory boundaries. This syntax is generally used for matching complete paths.
  • A question mark, ?, matches exactly one character.
  • Braces specify a collection of subpatterns. For example:
    • {sun,moon,stars} matches "sun", "moon", or "stars".
    • {temp*,tmp*} matches all strings beginning with "temp" or "tmp".
  • Square brackets convey a set of single characters or, when the hyphen character (-) is used, a range of characters. For example:
    • [aeiou] matches any lowercase vowel.
    • [0-9] matches any digit.
    • [A-Z] matches any uppercase letter.
    • [a-z,A-Z] matches any uppercase or lowercase letter.
    Within the square brackets, *, ?, and \ match themselves.
  • All other characters match themselves.
  • To match *, ?, or the other special characters, you can escape them by using the backslash character, \. For example: \\ matches a single backslash, and \? matches the question mark.
Here are some examples of glob syntax:

  • *.html – Matches all strings that end in .html
  • ??? – Matches all strings with exactly three letters or digits
  • *[0-9]* – Matches all strings containing a numeric value
  • *.{htm,html,pdf} – Matches any string ending with .htm, .html or .pdf
  • a?*.java – Matches any string beginning with a, followed by at least one letter or digit, and ending with .java
  • {foo*,*[0-9]*} – Matches any string beginning with foo or any string containing a numeric value

Note:If you are typing the glob pattern at the keyboard and it contains one of the special characters, you must put the pattern in quotes ("*"), use the backslash (\*), or use whatever escape mechanism is supported at the command line.

The glob syntax is powerful and easy to use. However, if it is not sufficient for your needs, you can also use a regular expression.
 

LucaMs

Expert
Licensed User
Longtime User
A crazy idea? Add a string builder to the class (a special, custom string builder, not to be confused with StringBuilder).

For example, I wouldn't have to remember (and write) the part:

[aeiou]

If I had an AddLowVowel.

Other: StartsWith - EndsWith - etc.
 

LucaMs

Expert
Licensed User
Longtime User
A crazy idea? Add a string builder to the class (a special, custom string builder, not to be confused with StringBuilder).

For example, I wouldn't have to remember (and write) the part:

[aeiou]

If I had an AddLowVowel.

Other: StartsWith - EndsWith - etc.

ChatGPT source code:
B4X:
' Classe per la generazione di stringhe Glob avanzata
Sub Class_Globals
    Private mStringBuilder As StringBuilder
    Private mGlobString As String
End Sub

' Inizializzazione della classe
Public Sub Initialize
    mStringBuilder.Initialize
    mGlobString = ""
End Sub

' Metodo per aggiungere un suffisso "*" alla fine della stringa
Public Sub AddEndsWith(suffix As String)
    mStringBuilder.Append(suffix).Append("*")
    mGlobString = mStringBuilder.ToString
End Sub

' Metodo per aggiungere "*" seguito dal parametro alla stringa
Public Sub AddStarsWith(prefix As String)
    mStringBuilder.Append("*").Append(prefix)
    mGlobString = mStringBuilder.ToString
End Sub

' Metodo per aggiungere le vocali minuscole "[aeiou]" alla stringa
Public Sub AddLowerVowel()
    mStringBuilder.Append("[aeiou]")
    mGlobString = mStringBuilder.ToString
End Sub

' Metodo per aggiungere le vocali maiuscole "[AEIOU]" alla stringa
Public Sub AddUpperVowel()
    mStringBuilder.Append("[AEIOU]")
    mGlobString = mStringBuilder.ToString
End Sub

' Metodo per aggiungere un set di consonanti minuscole "[bcdfghjklmnpqrstvwxyz]"
Public Sub AddLowerConsonant()
    mStringBuilder.Append("[bcdfghjklmnpqrstvwxyz]")
    mGlobString = mStringBuilder.ToString
End Sub

' Metodo per aggiungere un set di consonanti maiuscole "[BCDFGHJKLMNPQRSTVWXYZ]"
Public Sub AddUpperConsonant()
    mStringBuilder.Append("[BCDFGHJKLMNPQRSTVWXYZ]")
    mGlobString = mStringBuilder.ToString
End Sub

' Metodo per aggiungere qualsiasi carattere singolo "?"
Public Sub AddSingleCharacter()
    mStringBuilder.Append("?")
    mGlobString = mStringBuilder.ToString
End Sub

' Metodo per aggiungere qualsiasi numero di caratteri "*"
Public Sub AddAnyCharacters()
    mStringBuilder.Append("*")
    mGlobString = mStringBuilder.ToString
End Sub

' Metodo per aggiungere una sequenza specifica di caratteri
Public Sub AddExactSequence(sequence As String)
    mStringBuilder.Append(sequence)
    mGlobString = mStringBuilder.ToString
End Sub

' Metodo per aggiungere un range di numeri "[0-9]"
Public Sub AddNumberRange()
    mStringBuilder.Append("[0-9]")
    mGlobString = mStringBuilder.ToString
End Sub

' Metodo per aggiungere un range di lettere minuscole "[a-z]"
Public Sub AddLowerLetterRange()
    mStringBuilder.Append("[a-z]")
    mGlobString = mStringBuilder.ToString
End Sub

' Metodo per aggiungere un range di lettere maiuscole "[A-Z]"
Public Sub AddUpperLetterRange()
    mStringBuilder.Append("[A-Z]")
    mGlobString = mStringBuilder.ToString
End Sub

' Metodo per negare un insieme di caratteri "[^abc]"
Public Sub AddNegatedSet(chars As String)
    mStringBuilder.Append("[^").Append(chars).Append("]")
    mGlobString = mStringBuilder.ToString
End Sub

' Metodo per aggiungere un carattere letterale con escape "\"
Public Sub AddEscapedCharacter(char As String)
    mStringBuilder.Append("\").Append(char)
    mGlobString = mStringBuilder.ToString
End Sub

' Metodo per ottenere la stringa Glob costruita
Public Sub GetGlobString() As String
    Return mGlobString
End Sub

' Metodo per ripristinare la classe allo stato iniziale
Public Sub Clear()
    mStringBuilder.Initialize
    mGlobString = ""
End Sub


(Sorry, the comment lines are in Italian)
 
Top