Statistics subfolder size with PowerShell (improved version)

Source: Internet
Author: User

These two days changed to change before the PowerShell Statistics sub-folder code, in fact, is also an egg pain, simply look at the bad, the reason is that the previous code has not been able to achieve in the statistics folder size and see the effect of sorting, so it is inconvenient to see which folder is large, which folder is small. Thought should be very simple things, the results found that there is really a bit of trouble, the reason is that dir itself is not the size of the statistics folder, he will only count the simple file size, so it is inconvenient, initially want to use an array to store the size of the statistics, and then sort, after trying to find that is really feasible, But that's the problem. The statistics of the folder size is really with the sort effect, but the corresponding folder name to where?

It seems to want to achieve this effect must be the folder name and the size of the folder to form a set of corresponding relationship, such a correspondence look is not very familiar? This is the form of a hash table. But to tell the truth hash table usually own use of not much, to use when also really have to check data, in accordance with the form of a hash table changed, finally can be achieved. Such a small demand for a long time, it seems to have to temper the grammar of the Ah, correct later with you to share some more useful content

function FileSize () {[string] $filepath = read-host "Please enter folder path" $sortedlength = @{} $sorted = @{}if ($filepath-eq $null) {th Row "Path cannot be empty"}dir-path $filepath | foreach-object-process {if ($_.psiscontainer-eq $true) {$length = 0$name=$_.namedir-path $_.fullname-recurse | Foreach-object{[long] $length + = $_. Length} $sortedlength. ADD ($name, $length)}} $sorted = $sortedlength. GetEnumerator () | Sort-object Value-descendingforeach ($a in $sorted.  GetEnumerator ()) {if ($a. Value-ge 1MB) {$l = $a. Value/1mb$a.key + "folder size: {0:n1} MB"-f $l}else{$l = $a. Value/1kb$a.key + "The size of the folder is: {0:n1} KB"-F $l}}}filesizepause


This article is from the "Just Make It Happen" blog, so be sure to keep this source http://mxyit.blog.51cto.com/4308871/1656763

Statistics subfolder size with PowerShell (improved version)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.