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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Counting Files in directory

Status
Not open for further replies.

p7eggyc

Programmer
Dec 27, 2001
27
US
Good morning,
Have hunted around here and a couple of other forums and am not finding a simple solution to this problem. I am writing a module that will export some report data to excel for the user. I want to check the currentproject.path directory to assure they do not have more than 220 files in there already before allowing the export to occur. I can see the DIR function but don't really see any method to count the files. Do I have to actually create a counter and just loop through the folder using the DIR function to manually count them or is there a more efficient way?

Thanks!

Peggy
 
You could use the file system object and get the count from the files collection of the folder in question.


Public Function FileCount(ByVal strPath As String) As Integer
Dim objFSO As FileSystemObject

Set objFSO = New FileSystemObject

If objFSO.FolderExists(strPath) Then
FileCountInDirectory = objFSO.GetFolder(strPath).Files.Count
Else
'the folder does not exist
End If

Set objFSO = Nothing
End Function


You need to have a reference to Microsoft Scripting Runtime library.

Probably an easier way but hope this helps...
 
ooops,

FileCountInDirectory = objFSO.GetFolder(strPath).Files.Count

should be

FileCount = objFSO.GetFolder(strPath).Files.Count
 
Thanks for your help...had it my head I had to use DIR! Anyway, here is my code that is working. Had to change the declaration/initialization of the variable a little bit and the rest went great! Thanks again!

Peggy


Public Function FileCount(ByVal strPath As String) As Integer
Dim objFileSystemObject As Object


Set objFileSystemObject = CreateObject("Scripting.FileSystemObject")

If objFileSystemObject.FolderExists(strPath) Then
FileCount = objFileSystemObject.GetFolder(strPath).Files.Count
Else
'the folder does not exist
End If

Set objFileSystemObject = Nothing
End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top