code for counting files in a directory 
Author Message
 code for counting files in a directory

What is the correct code to count the number of files in a directory.  I
know how to use the dir loop, to get all files within the directory, but not
how to get a count of these.

Please help this troubled newbie.

Thanks in advance,

Chris Reddy



Wed, 07 May 2003 14:31:46 GMT  
 code for counting files in a directory
Hi Chris,

This function should do the trick! Call it in your code like this -
CountFilesInDir("C:\windows") - to count all the files (for example) in the
C:\Windows\ directory.

Public Function CountFilesInDir(Directory As String) As Long

  Dim Filename As String
  Dim Count As Long

  ' Check if the Directory has a trailing backslash
  If Right(Directory, 1) <> "\" Then Directory = Directory & "\"

  ' Set filename to equal the first file (if any) in the dir.
  Filename = Dir(Directory & "*.*")

  ' Loop until filename is null
  Do While Filename <> vbNullString

    ' Incrediment the count variable
    Count = Count + 1

    ' Get the next file
    Filename = Dir

  Loop

  CountFilesInDir = Count

End Function

Hope this helps.

--
--   Tom Hayward


Quote:
> What is the correct code to count the number of files in a directory.  I
> know how to use the dir loop, to get all files within the directory, but
not
> how to get a count of these.

> Please help this troubled newbie.

> Thanks in advance,

> Chris Reddy




Wed, 07 May 2003 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. code for counting files in a directory

2. How to get file count in directory

3. Counting Files in a Directory

4. Count all files in a directory tree

5. Count files in directory ?

6. help: Counting how many files in directory

7. Count files in a directory

8. Counting files in a directory???

9. Counting files in a Directory

10. HowTO : counting files in a directory using WinAPI ?

11. Counting files in a Directory

12. Code to count files and execute another program

 

 
Powered by phpBB® Forum Software