Hi,
How can I know how many files there are in a directory?
Printable View
Hi,
How can I know how many files there are in a directory?
you can use the FileSystemObject
or you can loop through a dir and put thenumber of loops in a counter using DIR function
*peets looking nerveously around... is Chris or Filburt here??? mmm.. no... .* u can use the FSO (File System Object) :)
VB Code:
'Add a reference to the 'Microsoft Scripting Runtime' (scrrun.dll) Option Explicit Dim fso As New FileSystemObject Dim fldr As Folder Private Sub Command1_Click() Set fldr = fso.GetFolder("c:\") MsgBox fldr.Files.Count End Sub
*peets looking nerveously around... is Chris or Filburt here??? mmm.. no... .* u can use the FSO (File System Object) :)
VB Code:
'Add a reference to the 'Microsoft Scripting Runtime' (scrrun.dll) Option Explicit Dim fso As New FileSystemObject Dim fldr As Folder Private Sub Command1_Click() Set fldr = fso.GetFolder("c:\") MsgBox fldr.Files.Count End Sub
see if this helps.
VB Code:
Public Sub PopulateArray(DirToSearch As String) Dim current As String Dim i As Integer Dim j As Integer i = 0 ReDim Ary(i) current = Dir(DirToSearch) Ary(i) = current While current <> "" i = i + 1 ReDim Preserve Ary(i) current = Dir Ary(i) = current Wend Debug.Print i End Sub
Seahag
One way:
usage:Code:Function filecnt(strDir as String) as Integer
dim i,tmp, loc
i = 0
tmp=dir(strDir & "\*.*")
do While tmp >""
i = i+1
tmp = dir
Loop
filecnt = i
End Function
filecount = filecnt("MyDir")
:cool:
Thanks a lot.
I'm going to try with the fso, but I didn't know
fso.files.count
When I write the dot, it doesn't give the Files
u have to have a folder ...
VB Code:
Set fldr = fso.GetFolder("c:\") MsgBox fldr.Files.Count
eeek.. references :(
OR WITHOUT USING THE FSO ...
Shove a filelistbox on your form (hide it if you need to), then
- set the folder to look in
- Return the file count
Done - in 2 lines of code. ;) DID I MENTION IT DOESN'T USE THE FSO ? :DCode:file1.path = "C:\myFolder\"
Msgbox file1.listcount & " files found"
LOL and LAC (Load And Clear :) ) Chris and Filburt will be jumping up and down in excitement ;)Quote:
Originally posted by alex_read
OR WITHOUT USING THE FSO ...
Shove a filelistbox on your form (hide it if you need to), then
- set the folder to look in
- Return the file count
Done - in 2 lines of code. ;) DID I MENTION IT DOESN'T USE THE FSO ? :DCode:file1.path = "C:\myFolder\"
Msgbox file1.listcount & " files found"
Lol
VB Code:
Function Test(SeverFolder As String, DestFolder As String) Dim oFileSystem Set oFileSystem = CreateObject("Scripting.FileSystemObject") oFileSystem.CopyFolder SeverFolder, DestFolder, True Set oFileSystem = Nothing End Function
?
Its called late binding.
instead of adding a ref. u use createobject.
when u do that, u don't need a ref.
I am confuse. Why do people hate FSO? they say its Bulky?
Any hints why?
People don't hate it , sheeps and turtles do :D
I do agree with them though. If u want to just count the number of files in a folder, u don't want to add yet another dll to u'r setup, making it even bigger :)
Normally I post FSO samples just to annoy Filburt and Chris :D but hey I'm an evil, evil, eeeeevil person :)
Yeah, I was just playing too, there's nothing really wrong with it, it just creates an extra reference in your code making your app a bit bigger & consuming.
It's great if there's a load of work involved - i.e. looping through 5 folders & pulling the files back from each, but just one off procedures like grabbing the amount of files from a folder - normal vb can do this ;)