How do I count files in a dir?

I know this question been up many times before, but please help me out!
Im new to VB and I am trying to make the program to do like this:

Count the number of files matching an input (1-121212.jpg)
if more than one number (1-121212.jpg, 2-121212.jpg, 3-121212.jpg)
then I need a msgbox to tell me this (and how many).

And please try to keep it simple!! ;)

Thank you/Jonas
tancredAsked:
Who is Participating?
 
aelatikConnect With a Mentor Commented:
As far as i could understand your Q this will help...

Dim sFilename As String
Dim Counter As Long
    Counter = 0
 sFilename = Dir("c:\*121212*.*") 'pattern
 Do Until Len(sFilename) = 0
   MsgBox sFilename
   sFilename = Dir()
   Counter = Counter + 1
 Loop
   MsgBox "Total Files : " & Counter
   
0
 
tancredAuthor Commented:
I only have 75 points to ask for... but this question is very omportant to me!!!
0
 
tejas_pathakCommented:
This code will display all the files that you specify in a directory of yours choice...

Call the Function as it is shown , with the parameter as
File to be find,Find Location (Directory),array name which to be filled with the found filenames.


Copy the foll. code in a standard module :

Option Explicit
Public Const MAX_PATH = 260

Public Type FILETIME
        dwLowDateTime As Long
        dwHighDateTime As Long
End Type

Public Type WIN32_FIND_DATA
        dwFileAttributes As Long
        ftCreationTime As FILETIME
        ftLastAccessTime As FILETIME
        ftLastWriteTime As FILETIME
        nFileSizeHigh As Long
        nFileSizeLow As Long
        dwReserved0 As Long
        dwReserved1 As Long
        cFileName As String * MAX_PATH
        cAlternate As String * 14
End Type

Public Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long
Public Declare Function FindNextFile Lib "kernel32" Alias "FindNextFileA" (ByVal hFindFile As Long, lpFindFileData As WIN32_FIND_DATA) As Long


Copy Foll. Code on to the Form :

Private Sub Command1_Click()
    Dim arr() As String
    Dim i As Long
   
    If FindFile("*tej*.txt", "c:\tejas", arr) = True Then
        For i = 0 To UBound(arr) - 1
            MsgBox arr(i)
        Next
    Else
        MsgBox "No Such File!!!"
    End If
End Sub

Private Function FindFile(FileName As String, Findin As String, ptrArr() As String) As Boolean
    Dim tmpData As WIN32_FIND_DATA
    Dim count As Long
    Dim retVal As Long
   
    ChDir Findin
   
    retVal = FindFirstFile(FileName, tmpData)
   
    If retVal = -1 Then
        FindFile = False
    Else
        count = 1
        ReDim ptrArr(count) As String
        ptrArr(count - 1) = Trim(tmpData.cFileName)
       
        While (FindNextFile(retVal, tmpData) <> 0)
            count = count + 1
            ReDim Preserve ptrArr(count) As String
            ptrArr(count - 1) = tmpData.cFileName
        Wend
       
        FindFile = True
    End If
End Function

Tell me more on this. If this is what you are looking for ?
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
tejas_pathakCommented:
This code will display all the files that you specify in a directory of yours choice...

Call the Function as it is shown , with the parameter as
File to be find,Find Location (Directory),array name which to be filled with the found filenames.


Copy the foll. code in a standard module :

Option Explicit
Public Const MAX_PATH = 260

Public Type FILETIME
        dwLowDateTime As Long
        dwHighDateTime As Long
End Type

Public Type WIN32_FIND_DATA
        dwFileAttributes As Long
        ftCreationTime As FILETIME
        ftLastAccessTime As FILETIME
        ftLastWriteTime As FILETIME
        nFileSizeHigh As Long
        nFileSizeLow As Long
        dwReserved0 As Long
        dwReserved1 As Long
        cFileName As String * MAX_PATH
        cAlternate As String * 14
End Type

Public Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long
Public Declare Function FindNextFile Lib "kernel32" Alias "FindNextFileA" (ByVal hFindFile As Long, lpFindFileData As WIN32_FIND_DATA) As Long


Copy Foll. Code on to the Form :

Private Sub Command1_Click()
    Dim arr() As String
    Dim i As Long
   
    If FindFile("*tej*.txt", "c:\tejas", arr) = True Then
        For i = 0 To UBound(arr) - 1
            MsgBox arr(i)
        Next
    Else
        MsgBox "No Such File!!!"
    End If
End Sub

Private Function FindFile(FileName As String, Findin As String, ptrArr() As String) As Boolean
    Dim tmpData As WIN32_FIND_DATA
    Dim count As Long
    Dim retVal As Long
   
    ChDir Findin
   
    retVal = FindFirstFile(FileName, tmpData)
   
    If retVal = -1 Then
        FindFile = False
    Else
        count = 1
        ReDim ptrArr(count) As String
        ptrArr(count - 1) = Trim(tmpData.cFileName)
       
        While (FindNextFile(retVal, tmpData) <> 0)
            count = count + 1
            ReDim Preserve ptrArr(count) As String
            ptrArr(count - 1) = tmpData.cFileName
        Wend
       
        FindFile = True
    End If
End Function

Tell me more on this. If this is what you are looking for ?
0
 
tancredAuthor Commented:
Yees!!  Thank you everyone!! This solves my problem!
/jonas
0
 
aelatikCommented:
Tancred,

If your problem is solved then accept the answer that helped you..
0
All Courses

From novice to tech pro — start learning today.