?
Solved

How do I count files in a dir?

Posted on 2003-03-26
6
Medium Priority
?
252 Views
Last Modified: 2010-04-17
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
0
Comment
Question by:tancred
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
6 Comments
 

Author Comment

by:tancred
ID: 8208889
I only have 75 points to ask for... but this question is very omportant to me!!!
0
 
LVL 14

Accepted Solution

by:
aelatik earned 150 total points
ID: 8209180
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
 

Expert Comment

by:tejas_pathak
ID: 8209321
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Expert Comment

by:tejas_pathak
ID: 8209322
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
 

Author Comment

by:tancred
ID: 8211157
Yees!!  Thank you everyone!! This solves my problem!
/jonas
0
 
LVL 14

Expert Comment

by:aelatik
ID: 8212839
Tancred,

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

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
A short article about problems I had with the new location API and permissions in Marshmallow
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Six Sigma Control Plans

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question