Take a look at this thread. It will show how to list all subdirectories. I am almost sure you can list all files the way I showed using the code as well. I don't know about getting the Directories sizes though.
Printable View
Take a look at this thread. It will show how to list all subdirectories. I am almost sure you can list all files the way I showed using the code as well. I don't know about getting the Directories sizes though.
Code:'Put this in a classmodule
Option Explicit
Event Filecatch(File As String, Path As String, level As Integer)
Event Direcatch(dire As String, Path As String, level As Integer)
Public curlevel%
Public filemax&
Public diremax&
Public tid&
Sub explore(startdir$, Optional pauses = 100)
Dim tids&
tids = Timer
filemax = 0: diremax = 0: curlevel = 0
If Right(startdir, 1) <> "\" Then startdir = startdir & "\"
SubFiles startdir, pauses
tid = Timer - tids
End Sub
Function SubFiles(Path$, Optional pauses = 100): Dim i&, dmax&, dirname$, dire$(), waiter&
dirname = Dir(Path, 63)
Do While dirname <> ""
If dirname <> "." And dirname <> ".." Then
If Int(GetAttr(Path + dirname) / 16) Mod 2 = 1 Then
If (dmax Mod 10) = 0 Then
ReDim Preserve dire(dmax + 10) ' Resize the array.
End If
diremax = diremax + 1: dmax = dmax + 1
dire(dmax) = dirname
RaiseEvent Direcatch(dirname, Path, curlevel)
Else
filemax = filemax + 1
RaiseEvent Filecatch(dirname, Path, curlevel)
End If
End If
dirname = Dir ' Get another directory name.
waiter = waiter + 1: If waiter Mod (pauses) = 1 Then DoEvents
Loop
For i = 1 To dmax
curlevel = curlevel + 1
SubFiles Path & dire(i) & "\"
Next i
curlevel = curlevel - 1
End Function
'And this in a form
Private WithEvents sd As subdirs
Public Dirsize As Long
Private Sub sd_Filecatch(File As String, Path As String, level As Integer)
Dirsize = Dirsize + FileLen(Path & "\" & File)
End Sub
Private Sub Form_Load()
Set sd = New subdirs
Dirsize = 0
sd.explore "C:\Windows"
MsgBox Dirsize
End Sub
Hi! Thanks for your help on this but I keep getting:
Compile Error:
User-defined type not defined
on the following line:
Private WithEvents sd As subdirs
Any ideas?
Cheers
Simon
You used my class? then you should rename it to "subdirs".
But i really recommend the API version, it's lot faster. But on the other hand if you want to have the files dropping in and speed is of no importance you could use my class
API? Where do I get that from :)
Simon
Or you could do like this:Quote:
Originally posted by Batman
But, it will only work on normal subdirectories. If subdirectories have attributes of read-only, your program does not work. You need to change all lines that are like this.
'Change to thisCode:If GetAttr(Temp & CurrentFolder) = vbDirectory Then
Code:If GetAttr(Temp & CurrentFolder) >= vbDirectory Then
Code:If (GetAttr(Temp & CurrentFolder) And vbDirectory) = vbDirectory Then
As kedaman said:
use the findfirst api to get first file in folder
then use findnext to get next till you have them all
in between all that as you get each file in the folder
you use FileLen(thefile) to get it's size and just keep
accumulating the totals
You can get the API FindFirst and FindNext here:
http://www.vbapi.com/ref/funca.html#f