|
-
Nov 28th, 2001, 10:08 AM
#1
Thread Starter
Lively Member
How many files?
Hi,
How can I know how many files there are in a directory?
-
Nov 28th, 2001, 10:11 AM
#2
Frenzied Member
you can use the FileSystemObject
or you can loop through a dir and put thenumber of loops in a counter using DIR function
-
Nov 28th, 2001, 10:13 AM
#3
-= B u g S l a y e r =-
*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
-
Nov 28th, 2001, 10:13 AM
#4
-= B u g S l a y e r =-
*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
-
Nov 28th, 2001, 10:15 AM
#5
Fanatic Member
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
-
Nov 28th, 2001, 10:15 AM
#6
One way:
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
usage:
filecount = filecnt("MyDir")
-
Nov 28th, 2001, 10:16 AM
#7
Fanatic Member
-
Nov 28th, 2001, 10:24 AM
#8
Thread Starter
Lively Member
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
-
Nov 28th, 2001, 10:27 AM
#9
-= B u g S l a y e r =-
u have to have a folder ...
VB Code:
Set fldr = fso.GetFolder("c:\")
MsgBox fldr.Files.Count
-
Nov 28th, 2001, 10:28 AM
#10
Fanatic Member
eeek.. references
-
Nov 28th, 2001, 10:31 AM
#11
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
Code:
file1.path = "C:\myFolder\"
Msgbox file1.listcount & " files found"
Done - in 2 lines of code. DID I MENTION IT DOESN'T USE THE FSO ?
-
Nov 28th, 2001, 03:05 PM
#12
-= B u g S l a y e r =-
-
Nov 28th, 2001, 03:25 PM
#13
Frenzied Member
-
Nov 28th, 2001, 04:09 PM
#14
Fanatic Member
Why do you not have to add a reference for this
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
?
-
Nov 28th, 2001, 04:30 PM
#15
-= B u g S l a y e r =-
Its called late binding.
instead of adding a ref. u use createobject.
when u do that, u don't need a ref.
-
Nov 28th, 2001, 04:45 PM
#16
Fanatic Member
I am confuse. Why do people hate FSO? they say its Bulky?
Any hints why?
-
Nov 28th, 2001, 04:49 PM
#17
-= B u g S l a y e r =-
-
Nov 29th, 2001, 03:30 AM
#18
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|