Folder and File Lister

traversd

Board Regular
Joined
Mar 17, 2002
Messages
82
Does anyone have an existing App or code for listing all the Folders in a directory by Size ?

My C:\ Is full and I want to identify the biggest folders for deleting files form....

Cheers
Darren
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
I thought when I started looking at this post that the answer would be fairly easy but it seems it's a little more complicated than that!

After a trawl through my knowledge and a major cobbling together of the VBA help available in Excel, here is an Excel macro that will do what you ask.

Copy this into a standard module, change (if necessary) the path you wish to interrogate and this should add a new workbook and place in Column A all the folder names and Column B, the respective sizes.

I have a feeling that the code is rather sloppy and I'm not really using the variables in the most efficient way. It works though so on that score - job done!

Cheers
AJ

<font face=Courier New><SPAN style="color:darkblue">Sub</SPAN> Folderinformation()

<SPAN style="color:green">'   Define variables</SPAN>
    <SPAN style="color:darkblue">Dim</SPAN> myPath <SPAN style="color:darkblue">As</SPAN> <SPAN style="color:darkblue">String</SPAN>, myFolder <SPAN style="color:darkblue">As</SPAN> <SPAN style="color:darkblue">String</SPAN>
    <SPAN style="color:darkblue">Dim</SPAN> myCounter <SPAN style="color:darkblue">As</SPAN> <SPAN style="color:darkblue">Long</SPAN>
    <SPAN style="color:darkblue">Dim</SPAN> fs, f
    
<SPAN style="color:green">'   Add new workbook for output of data and throw in some headings</SPAN>
    Workbooks.Add
    Range("A1").Value = "Folder Name"
    Range("B1").Value = "Folder Size"

<SPAN style="color:green">'   Specify variables</SPAN>
    myPath = "C:\"
    myCounter = 2
    <SPAN style="color:darkblue">Set</SPAN> fs = CreateObject("Scripting.FileSystemObject")

<SPAN style="color:green">'   Select specified drive and path</SPAN>
    ChDir myPath

<SPAN style="color:green">'   Get first entry</SPAN>
    myFolder = Dir(myPath, vbDirectory)

<SPAN style="color:green">'   Start looping through all folders</SPAN>
    <SPAN style="color:darkblue">Do</SPAN> <SPAN style="color:darkblue">While</SPAN> myFolder <> ""
    
<SPAN style="color:green">'       Make sure the current directory and the encompassing directory are ignored</SPAN>
        <SPAN style="color:darkblue">If</SPAN> myFolder <> "." And myFolder <> ".." <SPAN style="color:darkblue">Then</SPAN>

<SPAN style="color:green">'           Make sure MyFolder is actually a directory as the Dir command</SPAN>
<SPAN style="color:green">'           with vbDirectory also specifies files as well...</SPAN>
            <SPAN style="color:darkblue">If</SPAN> (GetAttr(myPath & myFolder) And vbDirectory) = vbDirectory <SPAN style="color:darkblue">Then</SPAN>

<SPAN style="color:green">'               Spit out folder name and size</SPAN>
                Cells(myCounter, 1) = myFolder
                <SPAN style="color:darkblue">Set</SPAN> f = fs.GetFolder(myFolder)
                Cells(myCounter, 2) = f.Size
                
<SPAN style="color:green">'               Increment the counter so the data collected goes on the next row in the output</SPAN>
                myCounter = myCounter + 1
            <SPAN style="color:darkblue">End</SPAN> <SPAN style="color:darkblue">If</SPAN>
        <SPAN style="color:darkblue">End</SPAN> <SPAN style="color:darkblue">If</SPAN>
    
<SPAN style="color:green">'       Get next one and carry on</SPAN>
        myFolder = Dir
        
    <SPAN style="color:darkblue">Loop</SPAN>

<SPAN style="color:darkblue">End</SPAN> <SPAN style="color:darkblue">Sub</SPAN>
</FONT>
 
Upvote 0
AJ,

That worked like a charm.....

How about a further challenge ? How to get it to search the subdirectories and look at the folders within those as well ?

The idea is to give a quick overview of which folders can be targeted quickly.

i.e. I know my TRW folder is the biggest in the Root directory C:, but I also know there's nowt I can delete in there !

Regards
Darren
 
Upvote 0
Here is a modification of a sub I had. You may have to fool around some with the offsets if you don't like the layout :)


Code:
Sub DetermineFolderSizes()
    Dim wbNew As Workbook
    Dim r As Range
    Dim startFolder As String
    
    startFolder = "c:\"
    
    Set wbNew = Workbooks.Add
    wbNew.Activate
    Set r = ActiveSheet.Cells(1, 1)
    
    FolderSizes startFolder, r
End Sub

Function FolderSizes(folder As String, r As Range) As Long
    Dim fs, f, f1, fc, sfc, sf, s
    Dim mySize As Range
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set f = fs.GetFolder(folder)
    Set sfc = f.Subfolders
    Set fc = f.Files
    
    r.Value = folder
    Set r = r.Offset(0, 1)
    Set mySize = r
    mySize.Value = 0
    
    For Each sf In sfc
        Set r = r.Offset(1, 0)
        mySize.Value = mySize.Value + FolderSizes(sf.Path, r)
    Next
    
    For Each f1 In fc
        mySize.Value = mySize.Value + FileLen(f1)
    Next
    
    Set r = r.Offset(0, -1)
    FolderSizes = mySize.Value
End Function

How it helps,
Goblin
 
Upvote 0
Hmmm, just ran than thing I posted above, and it doesn't quite cover todays HDD. Recommend dividing the filelength of each file by 1024 (KB), or even 1024^2 (MB)

Goblin
 
Upvote 0
Another option....

<font face=Courier New><SPAN style="color:#00007F">Option</SPAN> <SPAN style="color:#00007F">Explicit</SPAN>

<SPAN style="color:#00007F">Dim</SPAN> FSO <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Object</SPAN> <SPAN style="color:#007F00">'FileSystemObject</SPAN>

<SPAN style="color:#00007F">Sub</SPAN> Test()
    <SPAN style="color:#00007F">Dim</SPAN> Ar() <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Variant</SPAN>
    
    <SPAN style="color:#00007F">Set</SPAN> FSO = CreateObject("Scripting.FileSystemObject")   <SPAN style="color:#007F00">'New FileSystemObject</SPAN>
    
    <SPAN style="color:#007F00">'Constansts, change to do what you want</SPAN>
    <SPAN style="color:#00007F">Const</SPAN> Folder <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN> = "C:\"
    <SPAN style="color:#00007F">Const</SPAN> SearchSubFolders <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Boolean</SPAN> = <SPAN style="color:#00007F">True</SPAN>
    
    <SPAN style="color:#00007F">ReDim</SPAN> Ar(1 <SPAN style="color:#00007F">To</SPAN> 2, 1 <SPAN style="color:#00007F">To</SPAN> 1)
    Ar(1, 1) = "Folder Name"
    Ar(2, 1) = "Folder Size"
    GetFolderInfo Ar, Folder, SearchSubFolders
    <SPAN style="color:#00007F">Set</SPAN> FSO = <SPAN style="color:#00007F">Nothing</SPAN>
    Range("A1").Resize(UBound(Ar, 2), 2).Value = Application.Transpose(Ar)
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN>

<SPAN style="color:#00007F">Sub</SPAN> GetFolderInfo(<SPAN style="color:#00007F">ByRef</SPAN> Ar <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Variant</SPAN>, _
                       <SPAN style="color:#00007F">ByVal</SPAN> Folder <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>, _
                       <SPAN style="color:#00007F">ByVal</SPAN> SearchSubFolders <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Boolean</SPAN>)
    <SPAN style="color:#00007F">Dim</SPAN> Fl <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Object</SPAN>    <SPAN style="color:#007F00">'Folder</SPAN>
    <SPAN style="color:#00007F">Dim</SPAN> SubF <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Object</SPAN>  <SPAN style="color:#007F00">'Folder</SPAN>
    <SPAN style="color:#00007F">Dim</SPAN> i <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>
    
    <SPAN style="color:#00007F">Set</SPAN> Fl = FSO.GetFolder(Folder)
    i = <SPAN style="color:#00007F">UBound</SPAN>(Ar, 2) + 1
    <SPAN style="color:#00007F">ReDim</SPAN> <SPAN style="color:#00007F">Preserve</SPAN> Ar(1 <SPAN style="color:#00007F">To</SPAN> 2, 1 <SPAN style="color:#00007F">To</SPAN> i)
    <SPAN style="color:#00007F">If</SPAN> Fl.IsRootFolder <SPAN style="color:#00007F">Then</SPAN>
        Ar(1, i) = Fl.Path
    <SPAN style="color:#00007F">Else</SPAN>
        Ar(1, i) = Fl.Path
        Ar(2, i) = Fl.Size / 1024   <SPAN style="color:#007F00">'Kb</SPAN>
    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
    <SPAN style="color:#00007F">If</SPAN> SearchSubFolders <SPAN style="color:#00007F">Then</SPAN>
        <SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> SubF <SPAN style="color:#00007F">In</SPAN> Fl.SubFolders
            GetFolderInfo Ar, SubF.Path, SearchSubFolders
        <SPAN style="color:#00007F">Next</SPAN> SubF
    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
    <SPAN style="color:#00007F">Set</SPAN> Fl = <SPAN style="color:#00007F">Nothing</SPAN>
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>

I divide by 1024, but as goblin pointed, you may want to divide by 1024^2
 
Upvote 0
On the FolderInformation Sub, if I use a UNC as myPath then I get
a run-time error 76 "Path not found' ... line
Set f = fs.GetFolder(myFolder)

I only need to look at folders (and their size) within a directory.

Best Regards
 
Upvote 0

Forum statistics

Threads
1,223,974
Messages
6,175,739
Members
452,667
Latest member
vanessavalentino83

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top