Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

Count the number of files in a given directory

Total Hit ( 3242)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 


API Declarations

Click here to copy the following block
Option Explicit

'API constants
Public Const MAX_PATH = 260
Public Const INVALID_HANDLE_VALUE = -1
Public Const FILE_ATTRIBUTE_DIRECTORY = &H10

'API types
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

'API function calls
Public Declare Function FindFirstFile Lib "kernel32.dll" Alias "FindFirstFileA" (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long
Public Declare Function FindNextFile Lib "kernel32.dll" Alias "FindNextFileA" (ByVal hFindFile As Long, lpFindFileData As WIN32_FIND_DATA) As Long
Public Declare Function FindClose Lib "kernel32.dll" (ByVal hFindFile As Long) As Long

Module

Click here to copy the following block
Public Function NumFiles(sPath As String) As Long
  Dim f As WIN32_FIND_DATA
  Dim hFile As Long

  NumFiles = 0
  'Add the wildchar to the search path
  If Right(sPath, 1) <> "\" Then sPath = sPath & "\"
  sPath = sPath & "*.*"
  'start a file enum in the specified path
  hFile = FindFirstFile(sPath, f)
  If hFile = INVALID_HANDLE_VALUE Then Exit Function
  'Exclude subdirectories from count
  If (f.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY) = 0 Then NumFiles = 1
  Do While FindNextFile(hFile, f)
    If (f.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY) = 0 Then NumFiles = NumFiles + 1
  Loop
  'Close the file search
  FindClose (hFile)
End Function

Usage

Click here to copy the following block
'Usage:
Private Sub Command1_Click()
  MsgBox NumFiles("c:\") & " Files in C:\"
End Sub


Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.