I want to retrieve the number of files with the extention ".txt" in a folder.
Can somebody help me on this?
Tnx.
Dennie
Printable View
I want to retrieve the number of files with the extention ".txt" in a folder.
Can somebody help me on this?
Tnx.
Dennie
Add a FileListBox to your form, set the path (file1.path) to be the folder you are looking at. Set the pattern (file1.pattern) to *.txt, then just do file1.listcount to return the number of txt files.
Try this ...
Call with ...Code:Private Function GetNumberOfFiles(ByVal FolderName As String, _
ByVal Extension As String) As Long
Dim strFile As String
Dim lngCount As Long
lngCount = 0
strFile = Dir(FolderName, vbNormal Or vbReadOnly Or vbHidden Or vbSystem)
Do Until strFile = ""
If Right$(strFile, 3) = Extension Then lngCount = lngCount + 1
strFile = Dir
Loop
GetNumberOfFiles = lngCount
End Function
Hope this helps. :)Code:lngResult = GetNumberOfFiles("c:\SomeFolder\", "txt")
MsgBox lngResult
You should use the FindFiles API instead. This ones much faster but depending on the search area size, may take some time to.
The code is long for it but is availabel on good VB-Code sites. Try:
http://www.planet-source-code.com/xq...s/ShowCode.htm
'just a plain and simple count (No Extras)
Code:Private Sub Form_Load()
'access all files within a folder
Dim stFile As String
Dim stDir As String
Dim i As Integer
stDir = "C:\My Documents\" 'the folder to search
stFile = Dir$(stDir & "*.txt") 'type of files to select
Do While stFile <> ""
i = i + 1
stFile = Dir
Loop
'do what you want to with the information supplied
MsgBox "There are " & i & " files in " & _
stDir & " of txt type."
End Sub