Link to home
Start Free TrialLog in
Avatar of jonnyguitar
jonnyguitarFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Batch file or dos command to sort folders by size?

Hi there.  I need to sort a folder in order of size of sub-folder on Windows 2008.

I would like a batch file to do this or a dos / powershell command.  Does anyone know of any?

I do not want to install any software like Treesize etc.  I need to have this done in dos or Powershell.

Thanks!
Avatar of antony_kibble<!-8D58D5C365651885FB5A77A120C8C8C6-->
antony_kibble<!-8D58D5C365651885FB5A77A120C8C8C6-->

Dir /O:s   will display folder is smallest to largest order

Dir /O:-s    will display in largest to smallest
I have to disagree.  This is the correct syntax for sorting files, but the directories do not contain size information in DOS. You would have to use the /S to find what is inside those directories, but then it lists the sub directories separately.  I do not believe that you can use a DOS command to produce what jonnyguitar needs.

I had already run test with the above syntax and it does not sort the directories by size.
Avatar of jonnyguitar

ASKER

Thanks guys.  It didn't work.  Any other ideas?
Thanks.  That's cool but it's not what I need as it will only display files over a certain size inside a folder.

I have a folder with hundreds of sub-folders (user's home drives).  I need to archive the largest few folders so I need a way of finding this out.  Can the batch file be modified to accommodate?

Thanks!
I think that you could run the script below at the powershell prompt, Just substitute c:\temp with the root folder you want to sort

dir c:\temp -recurse | Where-Object { $_.PSIsContainer } | ForEach-Object { $_.FullName } | sort -descending
This is a good script but it sorts in reverse alphabetical order.  I need it to be list the sub-folder in order of size please (and if possible, with the size of the folder displayed too)
ASKER CERTIFIED SOLUTION
Avatar of wherami
wherami
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks.  When I run the script and enter a file path it simply doesn't do anything. Am I missing something?

Thanks
copy and paste the above code into notepad and save as "list.ps1" or whatever you want to it as. Start powershell and at the powershell prompt enter  ./list.ps1

PS C:\>./list.ps1
That's exactly what I did. And nothing happens after I enter the root folder and press enter...
Odd, if you run it on your local desktop does it work? May also want to replace the format-table auto with format-list
Ok now I get this error when running from desktop:


Exception calling "Add" with "2" argument(s): "Key cannot be null.
Parameter name: key"
At C:\files.ps1:16 char:16
+       $list.add <<<< ($Folder.FullName,$strSize)
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException
I think that is a result of a root folder path not found. Does the path name you enter exist? If you enter c:\ it will do you whole drive but it can take a few minutes depending on the size of the drive. I have a temp folder on my c drive and enter c:\temp for testing.