Get Folder List with VBA Dir – Remove Empty Folders
To get the list of folders within a folder through VBA, it would have been nice if the command name is VBA Dir.
But it is not like that. We have to read it by creating a object of Filesystemobject. If it is going to be plain list, then there are plenty of easy methods over there in internet. But, in this article we are also going to get the size of each folder also. Let’s see how to get it.
Remove Empty Directories – Is Easy Now
When we open Windows Explorer to view list of files and folders, it will only show File size, but not Folder Size. Try DIR command in CMD prompt and it is the same case there as well.
It will be a time consuming process to right click on each folder to check for its size. If it is a empty folder then we choose to delete it of keep it.
Here with this VBA Folder list code, we are also getting its size. Once we run this code, we get list of all folders with their size. Filter out folders with zero size and delete them. This will clear some unwanted placeholders easily.
- Create a New Excel Workbook. Enter a Folder Path in Sheet1->Cell A1.
- Press Alt + F11. This will open the VB Editor.
- Copy paste the below code.
- Run the code by Pressing F5. (Result will be printed in Sheet1 itself.)
Note: Before executing this code, include this reference “Microsoft Scripting Runtime” from Tools -> Reference. Refer MSDN for detailed information on FileSystemObject and Scripting Runtime.Visual Basic123456789101112131415161718192021222324252627Private Sub Find_Folder_List_From_Path()'Get Root Folder NameRoot_Path = ThisWorkbook.Sheets(1).Cells(1, 1)'Çreate File System Objects'To Use this FSO, Go to Tools -> References.'Find for “Microsoft Scripting Runtime” & Click the Checkbox.Dim FileSysObj_1 As FileSystemObjectDim Folder_Obj1 As Folder'Initialize Variables And Objectsi = 1Set FileSysObj_1 = New FileSystemObject'Loop to get each Subfolder in the Root PathFor Each Folder_Obj1 In FileSysObj_1.GetFolder(Root_Path).SubFoldersi = i + 1ThisWorkbook.Sheets(1).Cells(i, 2) = Folder_Obj1.NameOn Error Resume Next'Folder_obj1.Size returns value in Bytes.'1 KB = 1024 Bytes'1 MB = 1024 KB'1 GB = 1024 MBThisWorkbook.Sheets(1).Cells(i, 3) = (Folder_Obj1.Size / 1024) / 1024ThisWorkbook.Sheets(1).Cells(i, 4) = ((Folder_Obj1.Size / 1024) / 1024) / 1024NextEnd Sub
Once this code gets executed successfully, you will list of folders with size in sheet1. This list will be useful to decide what to do with folders of huge size to analyze whether it has any unwanted huge old data etc., and also you can remove empty folders if they are not required anymore.
Alternate Methods to Get Folder List
If the folder listing has to be done only for one time, the DOS command can be used. Just a plain DIR command placed inside a Batch file is enough, that can be executed whenever required.
- To list Subfolder and files use “dir /b” or “dir/b > D:dir/dir_list.txt” or
- For more graphical view use “tree” command in dos prompt)
Both ‘dir’ or ‘tree’ commands can be used in same manner and their outputs can be directed to a text file. If the folder list result is overflowing through the command prompt screen, then writing it to a text file and viewing it in a text editor would be the feasible one.
Removing Empty Folder is Now Easy – Get Folder List with Size
- Block Apps in FaceBook print screen Excel Countif Record Macro Timer in Excel Task Manager VCF HTML Table Import Data Thumbnails Text to Speech TTS Candy Crush C++ Programing Machine learning AI bulk email Excel VBA vba color index vba color codes Android social media Twitter Google+ Google Adsense Tips bitcoin Calendar in Excel outlook vba mass email Data Mining Excel Macro Facebook WhatsApp Wordpress python