Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Count the number of files in a directory

Status
Not open for further replies.

mark01

Technical User
Jan 17, 2001
600
US
How can I count the number of specific files in a directory. For example, I have the following files in C:\.
file1-temp1.txt
file2-temp1.txt
file3-temp1.txt
file1-temp2.txt

I only want to count the files which end in "temp1.txt" So from the files above, it would count a total of 3 files.

Thanks
 
Do a keyword search in this forum for 'Count files in directory' and then the 'Right function'. Then let us know what you come up with.

Two strings walk into a bar. The first string says to the bartender: 'Bartender, I'll have a beer. u.5n$x5t?*&4ru!2[sACC~ErJ'. The second string says: 'Pardon my friend, he isn't NULL terminated'.
 
Mark01,
Here's a nice function that does what you need:
Code:
[blue]Function[/blue] GetFiles(filespec [blue]As String[/blue], _
[blue]Optional[/blue] Attributes [blue]As VbFileAttribute[/blue]) [blue]As String[/blue]()

  [blue]Dim[/blue] result() [blue]As String[/blue]
  [blue]Dim[/blue] filename [blue]As String[/blue]
  [blue]Dim[/blue] count [blue]As Long[/blue], path2 [blue]As String[/blue]
  [blue]Const[/blue] ALLOC_CHUNK = 50
  [blue]ReDim[/blue] result(0 To ALLOC_CHUNK) [blue]As String[/blue]
  filename = Dir$(filespec, Attributes)
  [blue]Do While[/blue] Len(filename)
    count = count + 1
    [blue]If[/blue] count > UBound(result) [blue]Then[/blue]
      [green]' Resize the result array if necessary.[/green]
      [blue]ReDim Preserve[/blue] result(0 To count + _
      ALLOC_CHUNK) [blue]As String[/blue]
    [blue]End If[/blue]
    result(count) = filename
    [green]' Get ready for the next iteration.[/green]
    filename = Dir$
  [blue]Loop[/blue]
  [green]' Trim the result array.[/green]
  [blue]ReDim Preserve[/blue] result(0 To count) [blue]As String[/blue]
  GetFiles = result
[blue]End Function[/blue]

[green]' ...[/green]
[green]'--------------------------------------------
' This is how you would use it...[/green]
[blue]Dim[/blue] files() [blue]As String[/blue]
[blue]Dim[blue/] countFiles [blue]As Long[/blue]

[b]files = GetFiles("C:\YourDir\*.temp1.txt")
countFiles = Ubound(files)[/b]

MsgBox "There are " & countFiles.
[green]' ...[/green]
You could change it around to accomodate your needs!

JC

_________________________________________________
To get the best response to a question, read faq222-2244.
 
Code:
    Dim count As Long
    Dim lstrFile As String
    
    lstrFile = Dir("c:\*temp1.txt", vbNormal)
    
    Do While lstrFile <> ""
        count = count + 1
        lstrFile = Dir
    Loop
    
    MsgBox count & " files exist!"
 
Cool, I used bjd4jc's, and it worked!
Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top