当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 周五乐趣:文件夹的尺寸

作者:精品下载站 日期:2024-12-14 07:41:30 浏览:15 分类:玩电脑

周五乐趣:文件夹的尺寸


[玩转系统] 周五乐趣:文件夹的尺寸

这是完整的功能,之后我将指出几个关键点。

#requires -version 3.0

Function Measure-Folder {

<#
.SYNOPSIS
Measure the size of a folder.

.DESCRIPTION
This command will take a file path and create a custom measurement object that
shows the number of files and the total size. The default size will be in bytes
but you can specify a different unit of measurement. The command will format the
result accordingly and dynamically change the property name as well.

.PARAMETER Path
The default is the current path. The command will fail if it is not a FileSystem
path.

.PARAMETER Unit
The default unit of measurement is bytes, but you can use any of the standard
PowerShell numeric shortcuts: "KB","MB","GB","TB","PB"

.EXAMPLE
PS C:\> measure-folder c:\scripts 

Path            Name         Count         Size
----            ----         -----         ----
C:\scripts      scripts       2858     43800390

Measure the scripts folder using the default size of bytes.

.EXAMPLE

PS C:\> dir c:\scripts -Directory | measure-folder -Unit kb | Sort Size* -Descending | Select -first 5 | format-table -AutoSize

Path                     Name          Count          SizeKB
----                     ----          -----          ------
C:\scripts\GP            GP               40  2287.080078125
C:\scripts\Workflow      Workflow         64 1253.0185546875
C:\scripts\modhelp       modhelp           1  386.4970703125
C:\scripts\stuff         stuff             4       309.09375
C:\scripts\ADTFM-Scripts ADTFM-Scripts    76  297.7880859375

Get all the child folders under C:\scripts, measuring the size in KB. Sort the
results on the size property in descending order. Then select the first 5 objects
and format the results as a table.

.NOTES
Last Updated: 2/21/2014
Version     : 0.9

Learn more:
 PowerShell in Depth: An Administrator's Guide
 PowerShell Deep Dives 
 Learn PowerShell 3 in a Month of Lunches 
 Learn PowerShell Toolmaking in a Month of Lunches 

  ****************************************************************
  * DO NOT USE IN A PRODUCTION ENVIRONMENT UNTIL YOU HAVE TESTED *
  * THOROUGHLY IN A LAB ENVIRONMENT. USE AT YOUR OWN RISK.  IF   *
  * YOU DO NOT UNDERSTAND WHAT THIS SCRIPT DOES OR HOW IT WORKS, *
  * DO NOT USE IT OUTSIDE OF A SECURE, TEST SETTING.             *
  ****************************************************************

.LINK
https://jdhitsolutions.com/blog/2014/02/friday-fun-the-measure-of-a-folder

.LINK
Get-ChildItem
Measure-Object

.INPUTS
string or directory

.OUTPUTS
Custom object
#>
[cmdletbinding()]

Param(
[Parameter(Position=0,ValueFromPipeline=$True,
ValueFromPipelineByPropertyName=$True)]
[ValidateScript({Test-Path $_})]
[Alias("fullname")]
[string]$Path=".",

[ValidateSet("Bytes","KB","MB","GB","TB","PB")]
[string]$Unit="Bytes"
)

Begin {
    Write-Verbose -Message "Starting $($MyInvocation.Mycommand)"  
} #begin

Process {
    $Resolved = Resolve-Path -Path $path
    $Name = Split-Path -Path $Resolved -Leaf

    #verify we are in the file system
    if ($Resolved.Provider.Name -eq 'FileSystem') {

    #define a hash table to hold new object properties
    $propHash = [ordered]@{
    Path=$Resolved.Path
    Name=$Name
    }

Write-Verbose "Measuring $resolved in $unit"

$stats = Get-ChildItem -Path $Resolved -Recurse -File | Measure-Object -sum length

Write-Verbose "Measured $($stats.count) files"

$propHash.Add("Count",$stats.count)
Switch ($Unit) {

"bytes" { $propHash.Add("Size",$stats.sum)       ; break }
"kb"    { $propHash.Add("SizeKB",$stats.sum/1KB) ; break }
"mb"    { $propHash.Add("SizeMB",$stats.sum/1MB) ; break }
"gb"    { $propHash.Add("SizeGB",$stats.sum/1GB) ; break }
"tb"    { $propHash.Add("SizeTB",$stats.sum/1TB) ; break }
"pb"    { $propHash.Add("SizePB",$stats.sum/1PB) ; break }

} #switch

#write the new object to the pipeline
New-Object -TypeName PSobject -Property $propHash

}
else {
    Write-Warning "You must specify a file system path."
}

} #process

End {
    Write-Verbose -Message "Ending $($MyInvocation.Mycommand)"
} #end

} #end function

该命令将使用当前路径,或者您可以通过管道输入目录名称或 Get-ChildItem 表达式的输出,如我在帮助示例中所示。我添加的一件事是我测试该路径以确保它是文件系统路径,因为其他任何东西都不会真正起作用。

$Resolved = Resolve-Path -Path $path
$Name = Split-Path -Path $Resolved -Leaf

#verify we are in the file system
if ($Resolved.Provider.Name -eq 'FileSystem') {
    #define a hash table to hold new object properties
    $propHash = [ordered]@{
    Path=$Resolved.Path
    Name=$Name
    }
...
else {
        Write-Warning "You must specify a file system path."
    }

我解析路径,以便可以获得当前位置的实际名称 (.),然后测试提供程序。该函数的另一个有趣的功能是我可以即时格式化结果。

该函数有一个 Unit 参数,其默认值为字节。但您也可以指定 PowerShell 数字快捷方式之一,例如 KB 或 GB。在该函数中,我使用 Switch 构造来动态创建自定义属性。

Switch ($Unit) {

    "bytes" { $propHash.Add("Size",$stats.sum)       ; break }
    "kb"    { $propHash.Add("SizeKB",$stats.sum/1KB) ; break }
    "mb"    { $propHash.Add("SizeMB",$stats.sum/1MB) ; break }
    "gb"    { $propHash.Add("SizeGB",$stats.sum/1GB) ; break }
    "tb"    { $propHash.Add("SizeTB",$stats.sum/1TB) ; break }
    "pb"    { $propHash.Add("SizePB",$stats.sum/1PB) ; break }

 } #switch

由于 $Unit 只能有一个值,因此我包含了 Break 指令,以便 PowerShell 不会尝试处理 Switch 构造中的任何其他潜在匹配项。实际上,在这种情况下,性能提升可以忽略不计,但我想让您看看这是如何工作的。

PS C:\> dir D:\VM -Directory | measure-folder -Unit MB | Out-GridView -Title "VM Size"

[玩转系统] 周五乐趣:文件夹的尺寸

我采用了一些可以交互使用的基本命令,例如 Get-ChildItem 和 Measure-Object,并围绕它们构建了一个工具,使用属性哈希表来创建自定义对象。我希望你能从中得到一两个提示。如果您对我正在做的事情或原因有任何疑问,请随时提问,因为其他人可能也有同样的问题。

周末愉快!

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯