View Single Post
  #6   Report Post  
Posted to microsoft.public.excel.programming
Tom Ogilvy Tom Ogilvy is offline
external usenet poster
 
Posts: 6,953
Default Creating a list of Subfolders and sizes

Sub ShowFolderList(folderspec)
Dim fs, f, f1, fc, s, sSize
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFolder(folderspec)
Set fc = f.SubFolders
For Each f1 In fc

On Error Resume Next
sSize = Format(f1.Size, "#,##0")
If Err.Number < 0 Then
sSize = "NA"
End If
On Error GoTo 0
s = s & f1.Name & " " & sSize 'I added the "Size" bit
s = s & vbCrLf
Next
MsgBox s
End Sub

Sub test3()
folderspec = "C:\"
ShowFolderList (folderspec)
End Sub

worked fine for me.

the only problem with the original is that some folders choked on the size
command.

Also, a msgbox can only show 255 characters, so you string will be built,
but probably not entirely shown if you have a lot of subfolders.

--
Regards,
Tom Ogilvy


"Laurence Lombard" wrote:

I would like to be able to list the subfolders in any specified folder with
their respective sizes. In a search for a solution I copied this section
from the VBA's help, but this is not a standalone bit of code (why do they
not use standalone code for their examples?)

Sub ShowFolderList(folderspec)
Dim fs, f, f1, fc, s
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFolder(folderspec)
Set fc = f.SubFolders
For Each f1 In fc
s = s & f1.Name & " " & f1.Size 'I added the "Size" bit
s = s & vbCrLf
Next
MsgBox s

So I wrote this bit to call the above code

Sub test3()
folderspec = "C:\"
ShowFolderList (folderspec)
End Sub

However the line containing "Scripting.FileSystemObject" creates an error
"Runtime error 429 - ActiveX component can't create object"

What should folderspec be? How do I get it to work?

My VBA is self taught and I come unstuck when the help talks about
SearchFolders and ScopeFolders!

Thanks
Laurence