Count the Number of Files in a Folder

J

Jim

I’m sorry if this is a repost, I posted the same question yesterday but it
never appeared on the site.

Could someone please show me an example or examples of how to return the
number of files in a folder? I don’t need anything except the total number
of files in the folder. I’d like to use VB but, I’m open to any solution…

Thanks in advance,

Jim
 
R

ryguy7272

Try this:
Sub Demo()

MsgBox FileCountA("C:\temp\")
MsgBox FileCountB("C:\temp\")

End Sub

Function FileCountA(Path As String) As Long
Dim strTemp As String
Dim lngCount As Long

strTemp = Dir(Path & "*.*")
Do While strTemp <> ""
lngCount = lngCount + 1
strTemp = Dir
Loop

FileCountA = lngCount

End Function
Function FileCountB(Path As String) As Long

Dim objFSO As Object

Set objFSO = CreateObject("Scripting.FileSystemObject").GetFolder(Path)
FileCountB = objFSO.Files.Count

Set objFSO = Nothing

End Function
 
J

Jim

Thanks, function FileCountB works great!

Jim

ryguy7272 said:
Try this:
Sub Demo()

MsgBox FileCountA("C:\temp\")
MsgBox FileCountB("C:\temp\")

End Sub

Function FileCountA(Path As String) As Long
Dim strTemp As String
Dim lngCount As Long

strTemp = Dir(Path & "*.*")
Do While strTemp <> ""
lngCount = lngCount + 1
strTemp = Dir
Loop

FileCountA = lngCount

End Function
Function FileCountB(Path As String) As Long

Dim objFSO As Object

Set objFSO = CreateObject("Scripting.FileSystemObject").GetFolder(Path)
FileCountB = objFSO.Files.Count

Set objFSO = Nothing

End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top