jonnyguitar
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!
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!
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.
I had already run test with the above syntax and it does not sort the directories by size.
ASKER
Thanks guys. It didn't work. Any other ideas?
HaHa! very true, sorry got carried away there.
Try this answer previously
https://www.experts-exchange.com/questions/22638240/Sort-folders-in-a-directory-by-folder-size.html
Try this answer previously
https://www.experts-exchange.com/questions/22638240/Sort-folders-in-a-directory-by-folder-size.html
ASKER
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 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
dir c:\temp -recurse | Where-Object { $_.PSIsContainer } | ForEach-Object { $_.FullName } | sort -descending
ASKER
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks. When I run the script and enter a file path it simply doesn't do anything. Am I missing something?
Thanks
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
PS C:\>./list.ps1
ASKER
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
ASKER
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
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.
Dir /O:-s will display in largest to smallest