-
July 27th, 2006, 06:45 AM
#1
counting the number of files in a folder
i want to count the number of files present in a folder how can i do this
thank u in advance
-
July 27th, 2006, 06:56 AM
#2
Re: counting the number of files in a folder
Seems to me a good job for the FileSystemsObject.
Go to 'Project->References' and include 'Microsoft Scripting Runtime'
Then put this in a module
Code:
Public Function FileCount(PathName As String) As Long
Dim FSO As New FileSystemObject
Dim fld As Folder
If FSO.FolderExists(PathName) Then
Set fld = FSO.GetFolder(PathName)
FileCount = fld.Files.Count
End If
End Function
Call the function with a valid path like "C:\FolderName\AnotherFolder"
-
July 27th, 2006, 07:12 AM
#3
Re: counting the number of files in a folder
Fast & Quick WoF
Just for interest sake, you could also use the FindFirstFile and FindNextFile API's
As Explained Here:
http://www.allapi.net/apilist/FindFirstFile.shtml
-
July 27th, 2006, 07:38 AM
#4
Re: counting the number of files in a folder
Thanks
But I'd rather prefer the FSO for most file and folder operations.
You could also get the number of folders quickly: fld.SubFolders.Count
And you don't even need a function. If you have a public FileSystemObject somewhere it goes like:
if FSO.FolderExists(PathName) then NumFiles = FSO.GetFolder(PathName).Files.Count
I always have a statement Public FSO as New FileSystemObject on top of a module to have an FSO available throughout my program. It provides a whole lot of functions concerning files and folders including copying and deleting files and even complete folders which are so easy to access.
If you need more complex stuff like NameSpace objects try to reference 'Microsoft Shell Control And Automation' and look at the shell32.Folder and related objects. They are still quite easy to use.
-
July 27th, 2006, 07:53 AM
#5
Re: counting the number of files in a folder
Yeah, FSO is the preffered method - that's the whole reason why it exists. It is very very powerful. Just look how easy it makes our lives when we want to get file statistics.
Code:
Dim fso As New FileSystemObject
Dim fil As File
Dim strData As String
'Get a file by using the global variable filename
Set fil = fso.GetFile(gf_strOpenFile)
'Get the filename
strData = "Name: " & fil.Name & vbCrLf
'Get the date created
strData = strData & "Created: " & fil.DateCreated & vbCrLf
'Get the date last modified
strData = strData & "Modifed: " & fil.DateLastModified & vbCrLf
'Get the date last accessed
strData = strData & "Last Access: " & fil.DateLastAccessed & vbCrLf
'Get the size in bytes
strData = strData & "Size: " & CStr(fil.Size) & " characters" & vbCrLf
'Get the file type
strData = strData & "Type: " & fil.Type & vbCrLf
'Get the parent folder
strData = strData & "Parent: " & fil.ParentFolder.Name & vbCrLf
'Get the file path
strData = strData & "Path: " & fil.Path & vbCrLf
'Report the findings
MsgBox strData, vbOKCancel, "File Statistics"
API is also quite fast, but sometimes, like here, it's code is a bit too long and complicated.
-
July 27th, 2006, 08:18 AM
#6
Re: counting the number of files in a folder
Yes, very good usage of the FSO, Hannes.
I also appreciate the TextStream functions which make it sooo easy to read and write from text files without all that Open FileName for Input (or Output) As FileNumber stuff.
If you have already hold of a File object (in fil) you can
Code:
dim a$, ts as TextStream
set ts = fil.OpenAsTextStream
a=ts.ReadAll
ts.close
It really helps to learn all about the File and Folder objects.
-
July 28th, 2006, 12:15 AM
#7
Re: counting the number of files in a folder
-
July 31st, 2006, 06:49 AM
#8
Re: counting the number of files in a folder
i have one more problem related to this
that is i want to write a function that computes the size of files in a folder that is during first call to function it should compute the size of first 10 files in the folder(1-10) during next function call it should compute the sizes of next 10 files in that folder and so on untill all files are finished is it possible to accomplish this in VB
-
July 31st, 2006, 10:31 AM
#9
Re: counting the number of files in a folder
This is seems to me a rather strange request as I do not know how you want to use this. Also files come not necessarily always in multiples of 10.
But I'd suggest a different approach. Why not creating a dynamic array with all sizes of all files and then add them up in groups as you need them.
Code:
Public Sub FillFileSizes(PathName As String)
' assuming you have a public FileSizes() as String
Dim fil As File
Dim fld As Folder
Dim cnt%
Set fld = FSO.GetFolder(PathName)
If Not fld Is Nothing Then
If fld.Files.Count Then
ReDim FileSizes(fld.Files.Count)
For Each fil In fld.Files
FileSizes(cnt) = fil.Size
cnt = cnt + 1
Next
End If
End If
' UBound(FileSizes) will then reflect the number of files
' the array is always one element larger since it starts with element 0
' but that does not matter
End Sub
Gives you the opportunity to do with the filesizes stored in array FileSizes() whatever you want. Add them up in tenths, or whatever.
-
July 31st, 2006, 11:23 PM
#10
Re: counting the number of files in a folder
i think u misundestood my problem
what i want to do is,
i want to get the total size of first ten files during first call to function.
During second call i want to get the total size of next 10 files (11-20)
i am passing one index to the function
if index=1 then total size of 1st to 10 files
if index=2 then total size of 11th to 20 files
if index=3 then total size of 21th to 30 files
and so on untill all files in that folder are finished
during last call the number of files may be less than 10.
-
August 1st, 2006, 12:05 AM
#11
Re: counting the number of files in a folder
Sounds like homework, to me.
Why would you want to know the size of 10 files, when you wouldn't know which ones they were? If you wanted to move them into folders, that be different.
Please explain further.
-
August 1st, 2006, 12:30 AM
#12
Re: counting the number of files in a folder
i got the answer but i want to do it in a efficient way here is my code
Dim FSO As Scripting.FileSystemObject Runtime
Set FSO = New Scripting.FileSystemObject
Dim file As Scripting.file
Dim Folder As Scripting.Folder
Dim Size As Long
Dim lower, upper As Integer
Dim i As Integer
i = 1
upper = index * 10
lower = upper - 10+ 1
Set Folder = FSO.GetFolder(PathName)
For Each file In Folder.Files
If i >= lower And i <= upper Then
Size = Size + file.Size
FILESIZE = Size
End If
i = i + 1
If i > upper Then
GoTo lab
End If
Next
lab:
-
August 1st, 2006, 01:34 AM
#13
Re: counting the number of files in a folder
You aren't doing anything with FileSize
I'd do it like this:
Code:
Option Explicit
Private Sub Form_Load()
Dim FSO As Scripting.FileSystemObject Runtime
Set FSO = New Scripting.FileSystemObject
Dim file As Scripting.file
Dim Folder As Scripting.Folder
Dim Size As Long
Dim I as Integer ' < 32k Records <-------------------------
Set Folder = FSO.GetFolder(PathName)
For Each file In Folder.Files
Size = Size + file.Size
If i Mod 10 = 0 Then ' <-------- Stop every 10th file
MsgBox x - 9 & " - " & x & " = " & size ' Do something with total
Size = 0 ' Reset total (if you don't want running totals)
End If
i = i + 1
Next
End Sub
-
August 1st, 2006, 03:52 AM
#14
Re: counting the number of files in a folder
there FILESIZE is the function name it is for returning the value after countig the size of ten files
-
August 1st, 2006, 04:40 AM
#15
Re: counting the number of files in a folder
I think, harish, the solution you got is quite alright. It does exactly what you want, even if I don't understand why you would need this. Just little cosmetics:
Code:
For Each file In Folder.Files
If i >= lower And i <= upper Then then Size = Size + file.Size
i = i + 1
If i > upper Then Exit For 'this looks better than GoTo
Next
FILESIZE = Size 'you need assign the return value only once when you are done
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
|