B4J Question List only txt files in a directory

ThRuST

Well-Known Member
Licensed User
Longtime User
As the headline says, how to list only .txt files in a directory with B4J?

This is what I tried

B4X:
Dim fl As List = File.ListFiles("c:\")
    For j = 0 To fl.Size-1
        Log(fl.Get(j))
        ListView1.Items.Add(fl.Get(j))
    Next

But it lists also the directories. One idea might be to use File.IsDirectory but I dunno how to implement it.
 

Cableguy

Expert
Licensed User
Longtime User
Take each filename, start from the end, if the 4th position from the end is a dot, check the last 3 characters! Add only the filenames that end by ".txt"
 
Upvote 0

ThRuST

Well-Known Member
Licensed User
Longtime User
Thank you @Cableguy I came up with this solution. It's kinda clever :)

B4X:
Sub ListFiles
 
    For Each FileName As String In File.ListFiles("c:\")
        Log(FileName)
        If FileName.Contains(".txt") Then
            ListView1.Items.Add(FileName)
        End If
    Next
 
End Sub

Edit: Just use "." to list all the files.
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
Then a file like "my.txt.bak" will also be listed.using your approach, cut the 4 last characters from the original filename into a new string like:

Dim FileExtension as string = Filename.Substring2(filename.length-4, filename.length)
If FileExtension = ".txt" then...
 
Upvote 0

ThRuST

Well-Known Member
Licensed User
Longtime User
Ok ok have it your way, you win (again) :D
May we have the complete votes (solution), France ? :)
 
Upvote 0

udg

Expert
Licensed User
Longtime User
Once I did something similar to look up for images in a dir and its subdirs. For Windows I used code like:
B4X:
'Search for a specific file extension
Private Sub CollectFileExt(fExt As String)
   Dim shl As Shell
   shl.Initialize("shltst","cmd",Array As String("/C","dir","/b","/s",root&"*."&fExt))
   'shl.WorkingDirectory="C:\"
   shl.Run(Timeout)
End Sub

root = starting dir; timeout = used to refresh a label sporting the count of found objects

udg
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
It was pseudo code, the kind of code only genius understand! ;)
 
Upvote 0

ThRuST

Well-Known Member
Licensed User
Longtime User
@udg I tried this maybe I made a misstake how to use your code. Please correct me!

B4X:
CollectFileExt(".txt")

B4X:
'Search for a specific file extension
Private Sub CollectFileExt(fExt As String)
    Dim shl As Shell
    shl.Initialize("shltst","cmd",Array As String("/C","dir","/b","/s","c:\test" & "*." & fExt))
    'shl.WorkingDirectory="C:\"
    shl.Run(10000)
End Sub

Notice that I changed root to c:\test
 
Upvote 0

udg

Expert
Licensed User
Longtime User
CollectFileExt("txt") <-- without initial dot
root should be "c:\test\" <-- add final backslash

B4X:
Private Sub shltst_ProcessCompleted (Success As Boolean, ExitCode As Int, StdOut As String, StdErr As String)
  If Success And ExitCode = 0 Then
     Log("Success")
     Log(StdOut)  'better to comment out in production
      'insert valid data in ImgList
      Dim separator As String
      separator = Chr(13)&Chr(10)
      ImgList.AddAll(Regex.Split(separator,StdOut))
  Else
    Log("Error: " & StdErr)
  End If
end sub
 
Upvote 0

ThRuST

Well-Known Member
Licensed User
Longtime User
And here we have the solution provided by me and @Cableguy

B4X:
Sub ListFiles
   
    Try
   
        For Each FileName As String In File.ListFiles("c:\")
               
                Dim FileExtension As String = FileName.Substring2(FileName.length-4, FileName.length)
                If FileExtension = ".txt" Then
                    ListView1.Items.Add(FileName)
                End If
        Next
           
    Catch
        Log(LastException)
    End Try
   
End Sub
 
Upvote 0

ThRuST

Well-Known Member
Licensed User
Longtime User
@ThRuST genious edit ;)

B4X:
ListFiles("c:\", ".txt")

B4X:
Sub ListFiles(DrvPath As String, Xtension As String)
   
    Try
   
        For Each FileName As String In File.ListFiles(DrvPath)
               
                Dim FileExtension As String = FileName.Substring2(FileName.length-4, FileName.length)
                If FileExtension = Xtension Then
                    ListView1.Items.Add(FileName)
                End If
        Next
           
    Catch
        Log(LastException)
    End Try
   
End Sub
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

jmon

Well-Known Member
Licensed User
Longtime User
Thank you @Cableguy I came up with this solution. It's kinda clever :)
B4X:
Sub ListFiles
    For Each FileName As String In File.ListFiles("c:\")
        Log(FileName)
        If FileName.Contains(".txt") Then
            ListView1.Items.Add(FileName)
        End If
    Next
End Sub
this would be better:
B4X:
Sub ListFiles
    For Each FileName As String In File.ListFiles("c:\")
        If FileName.ToLowerCase.EndsWith(".txt") Then
            ListView1.Items.Add(FileName)
        End If
    Next
End Sub
 
Upvote 0

Robert Valentino

Well-Known Member
Licensed User
Longtime User
@ThRuST genious edit ;)

B4X:
ListFiles("c:\", ".txt")

B4X:
Sub ListFiles(DrvPath As String, Xtension As String)
  
    Try
  
        For Each FileName As String In File.ListFiles(DrvPath)
              
                Dim FileExtension As String = FileName.Substring2(FileName.length-4, FileName.length)
                If FileExtension = Xtension Then
                    ListView1.Items.Add(FileName)
                End If
        Next
          
    Catch
        Log(LastException)
    End Try
  
End Sub


You are doing FileName.length-4 to get the extension. File extension can be more then 3 characters like ".jpeg" not normally but can be.

I would look for the last "." - like doing

Dim LastPeriod as Int = LastIndexOf(".")

if LastPeriod <> -1 then
Dim FileExtension as String = FileName.SubString(LastPeriod)
end if

Just my 2 cents
 
Upvote 0
Top