ListFilesBySize VBS script

Blue Bar separator

Where was all my disk space going? I went to google and searched for something that would tell me and couldn't find anything I liked. So I wrote this simple VBS script that will list all files and directories starting at a given point and sort them by size. Note this script does not account for space taken by Alternate Data Streams.

To execute this script open a command window and type "cscript listfilesbysize START OUT". Replace START with the path of the directory you want to start in and OUT with the base name of the output file to be created. Assuming that there are no errors with the execution the actual output file will have the name OUT.sorted. Bear in mind that the more files and directories you have the longer it takes for this command to finish. It took over 6 minutes to scan my C:\ drive containing slightly over 28 GB "Used Bytes".

Example 1 - List all files in the current directory and all subdirectory and place the outut in a file called x.sorted

H:\projects\VB scripts>cscript listfilesbysize.vbs . x
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.


H:\projects\VB scripts>
The x.sorted file looks this. I have removed a great many lines but you should get the idea. Note that files are listed first then directories. Directories have a "D" at the begining of the line.
                 This run of ListFilesBySize done at 9/23/2006 6:30:51 PM
               0	H:\projects\VB scripts\x
              43	H:\projects\VB scripts\Win32_NetworkAdapterConfiguration_file\leftframe_file\dtbar_file\1pix.gif
              43	H:\projects\VB scripts\Win32_NetworkAdapterConfiguration_file\leftframe_file\search_file\1pix.gif
              43	H:\projects\VB scripts\Win32_NetworkAdapterConfiguration_file\rightframe_file\contentbar_file\1pix.gif
              67	H:\projects\VB scripts\Win32_NetworkAdapterConfiguration_file\leftframe_file\dtbar_file\hidetoc1.gif
                                              . . . 
           37102	H:\projects\VB scripts\Win32_NetworkAdapterConfiguration_file\toolbar_file\toolbar.js
           77562	H:\projects\VB scripts\Win32_NetworkAdapterConfiguration_file\rightframe_file\win32_networkadapterconfiguration.htm
D             615	H:\projects\VB scripts\Win32_NetworkAdapterConfiguration_file\leftframe_file\dtbar_file
D            2716	H:\projects\VB scripts\Win32_NetworkAdapterConfiguration_file\leftframe_file\deeptree_file
D            3127	H:\projects\VB scripts\Win32_NetworkAdapterConfiguration_file\rightframe_file\contentbar_file
D            5558	H:\projects\VB scripts\Win32_NetworkAdapterConfiguration_file\leftframe_file\search_file
D           20165	H:\projects\VB scripts\Win32_NetworkAdapterConfiguration_file\leftframe_file
                                             . . .
D          235863	H:\projects\VB scripts\Win32_NetworkAdapterConfiguration_file

Example 2 - List all files on the C drive and place the output in the file c:\c.sorted

H:\projects\VB scripts>cscript listfilesbysize.vbs c:\ c:\c
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.


H:\projects\VB scripts>
I'm not going to list the contents of c.sorted. Other than the file paths the contents of c.sorted and x.sorted look pretty much the same.

ListFilesBySize.vbs

                                                                                                       
REM ListFilesBySize.vbs begins here
REM
REM Version 1.0 06-09-23
REM Version 1.1 10-11-26  Added disclaimer
REM Noah Davids - ndav1@cox.net
REM
REM
REM This software is provided on an "AS IS" basis, WITHOUT ANY WARRANTY OR ANY SUPPORT OF ANY KIND. 
REM The AUTHOR SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY
REM PARTICULAR PURPOSE.  This disclaimer applies, despite any verbal representations of any kind provided
REM by the author or anyone else.

option explicit
dim objFSO
dim outFilePath
dim outFile
const ForWriting = 2
const pad = "               "
dim WshShell

On Error Resume Next

if WScript.Arguments.Count <> 2 then
   WScript.Echo "Usage:"
   WScript.Echo vbtab & "cscript ListFilesBySize.vbs  "
   WScript.Echo
else
   Set objFSO = CreateObject ("Scripting.FileSystemObject")

   if instr (WScript.Arguments (1), ":") = 2 then
      outFilePath = WScript.Arguments (1)
   else
      if mid (WScript.Arguments (0), Len (WScript.Arguments (0)), 1) = "\" then
         outFilePath = WScript.Arguments (0) & WScript.Arguments (1)
      else
         outFilePath = WScript.Arguments (0) & "\" & WScript.Arguments (1)
      end if
   end if

   if objFSO.FileExists (outFilePath) then
      set outFile = objFSO.OpenTextFile (outFilePath, ForWriting)
   else
      set outFile = objFSO.CreateTextFile (outFilePath)
      outFile.Close
      set outFile = objFSO.OpenTextFile (outFilePath, ForWriting)
   end if

   outFile.WriteLine pad & "  This run of ListFilesBySize done at " & Now

   Set WshShell = WScript.CreateObject ("WScript.Shell")

   ProcessFolder (WScript.Arguments (0))

   outFile.Close

   Set WshShell = WScript.CreateObject ("WScript.Shell")
   WshShell.Run "Cmd.exe /c sort """ & outFilePath & """ /o """ & outFilePath & ".sorted""", 0 , True
   objFSO.DeleteFile (outFilePath)
   WshShell.Run "Cmd.exe /c rename """ & outFilePath & ".sorted"" """ & outFilePath & """", 0 , True
end if

sub ProcessFolder (AFolder)

dim objFSO
dim objFolder
dim colFiles
dim AFile
dim colFolders
dim AFolder2
dim temp
dim i

set objFSO = CreateObject ("Scripting.FileSystemObject")
set objFolder = objFSO.GetFolder (AFolder)
set colFiles = objFolder.Files

for each AFile in colFiles
    temp = AFile.Size
    i = 0
    do until temp < 10
       temp = temp / 10
       i = i + 1
    Loop
    outFile.WriteLine mid (pad, 1, 15 - i) & AFile.Size & vbTab & AFile.Path
next

set colFolders = objFolder.SubFolders
for each AFolder2 in colFolders
    temp = AFolder2.Size
    i = 0
    do while temp > 1
       temp = temp / 10
       i = i + 1
    Loop
    if i = 0 then i = 1
    outFile.WriteLine "D " & mid (pad, 1, 15 - i) & AFolder2.Size & vbTab & AFolder2.Path
    
    ProcessFolder (AFolder2)
next

end sub
REM
REM ListFilesBySize.vbs ends here



Blue Bar separator
This page was last modified on 10-11-26
mailbox Send comments and suggestions
to ndav1@cox.net