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"
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
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 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
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
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
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
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
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
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