用于获取目录总大小的 PowerShell 脚本

发布于 2024-07-18 14:18:46 字数 162 浏览 6 评论 0原文

我需要递归地获取目录的大小。 我每个月都必须执行此操作,因此我想制作一个 PowerShell 脚本来执行此操作。

我该怎么做?

I need to get the size of a directory, recursively. I have to do this every month so I want to make a PowerShell script to do it.

How can I do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

心清如水 2024-07-25 14:18:46

尝试以下操作

function Get-DirectorySize() {
  param ([string]$root = $(resolve-path .))
  gci -re $root |
    ?{ -not $_.PSIsContainer } | 
    measure-object -sum -property Length
}

这实际上会生成一些摘要对象,其中将包括项目计数。 不过,您可以只获取 Sum 属性,这将是长度的总和

$sum = (Get-DirectorySize "Some\File\Path").Sum

编辑为什么这有效?

让我们按管道的组成部分来分解它。 gci -re $root 命令将从起始 $root 目录递归获取所有项目,然后将它们推入管道。 因此,$root 下的每个文件和目录都将通过第二个表达式 ?{ -not $_.PSIsContainer }。 传递给此表达式的每个文件/目录都可以通过变量 $_ 进行访问。 前面的? 指示这是一个过滤器表达式,意味着仅保留管道中满足此条件的值。 PSISContainer 方法将为目录返回 true。 因此,实际上过滤器表达式仅保留文件值。 最终的 cmdlet 测量对象将对管道中剩余的所有值的属性 Length 的值进行求和。 因此,它本质上是对当前目录下的所有文件调用 Fileinfo.Length (递归)并对值求和。

Try the following

function Get-DirectorySize() {
  param ([string]$root = $(resolve-path .))
  gci -re $root |
    ?{ -not $_.PSIsContainer } | 
    measure-object -sum -property Length
}

This actually produces a bit of a summary object which will include the Count of items. You can just grab the Sum property though and that will be the sum of the lengths

$sum = (Get-DirectorySize "Some\File\Path").Sum

EDIT Why does this work?

Let's break it down by components of the pipeline. The gci -re $root command will get all items from the starting $root directory recursively and then push them into the pipeline. So every single file and directory under the $root will pass through the second expression ?{ -not $_.PSIsContainer }. Each file / directory when passed to this expression can be accessed through the variable $_. The preceding ? indicates this is a filter expression meaning keep only values in the pipeline which meet this condition. The PSIsContainer method will return true for directories. So in effect the filter expression is only keeping files values. The final cmdlet measure-object will sum the value of the property Length on all values remaining in the pipeline. So it's essentially calling Fileinfo.Length for all files under the current directory (recursively) and summing the values.

彡翼 2024-07-25 14:18:46

如果您有兴趣包含隐藏文件和系统文件的大小,那么您应该将 -force 参数与 Get-ChildItem 一起使用。

If you are interested in including the size of hidden and system files then you should use the -force parameter with Get-ChildItem.

那伤。 2024-07-25 14:18:46

以下是获取特定文件扩展名大小的快速方法:

(gci d:\folder1 -r -force -include *.txt,*.csv | measure -sum -property Length).Sum

Here's quick way to get size of specific file extensions:

(gci d:\folder1 -r -force -include *.txt,*.csv | measure -sum -property Length).Sum
彩虹直至黑白 2024-07-25 14:18:46

感谢那些在这里发帖的人。 我采用了知识来创建这个:

# Loops through each directory recursively in the current directory and lists its size.
# Children nodes of parents are tabbed

function getSizeOfFolders($Parent, $TabIndex) {
    $Folders = (Get-ChildItem $Parent);     # Get the nodes in the current directory
    ForEach($Folder in $Folders)            # For each of the nodes found above
    {
        # If the node is a directory
        if ($folder.getType().name -eq "DirectoryInfo")
        {
            # Gets the size of the folder
            $FolderSize = Get-ChildItem "$Parent\$Folder" -Recurse | Measure-Object -property length -sum -ErrorAction SilentlyContinue;
            # The amount of tabbing at the start of a string
            $Tab = "    " * $TabIndex;
            # String to write to stdout
            $Tab + " " + $Folder.Name + "   " + ("{0:N2}" -f ($FolderSize.Sum / 1mb));
            # Check that this node doesn't have children (Call this function recursively)
            getSizeOfFolders $Folder.FullName ($TabIndex + 1);
        }
    }
}

# First call of the function (starts in the current directory)
getSizeOfFolders "." 0

Thanks to those who posted here. I adopted the knowledge to create this:

# Loops through each directory recursively in the current directory and lists its size.
# Children nodes of parents are tabbed

function getSizeOfFolders($Parent, $TabIndex) {
    $Folders = (Get-ChildItem $Parent);     # Get the nodes in the current directory
    ForEach($Folder in $Folders)            # For each of the nodes found above
    {
        # If the node is a directory
        if ($folder.getType().name -eq "DirectoryInfo")
        {
            # Gets the size of the folder
            $FolderSize = Get-ChildItem "$Parent\$Folder" -Recurse | Measure-Object -property length -sum -ErrorAction SilentlyContinue;
            # The amount of tabbing at the start of a string
            $Tab = "    " * $TabIndex;
            # String to write to stdout
            $Tab + " " + $Folder.Name + "   " + ("{0:N2}" -f ($FolderSize.Sum / 1mb));
            # Check that this node doesn't have children (Call this function recursively)
            getSizeOfFolders $Folder.FullName ($TabIndex + 1);
        }
    }
}

# First call of the function (starts in the current directory)
getSizeOfFolders "." 0
遮了一弯 2024-07-25 14:18:46

要完善@JaredPar的这个答案以进行扩展和提高性能:

function Get-DirectorySize() {
  param ([string]$root = $(Resolve-Path .))
  Get-ChildItem $root -Recurse -File |
    Measure-Object -Property Length -Sum |
    Select-Object -ExpandProperty Sum
}

或者,为了使其更方便使用,请探索类型数据:

Update-TypeData -TypeName System.IO.DirectoryInfo -MemberType ScriptProperty -MemberName Size -Value {
  Get-ChildItem $this -Recurse -File |
    Measure-Object -Property Length -Sum |
    Select-Object -ExpandProperty Sum
}

然后通过 Get-ChildItem | 使用 选择-对象名称、长度、大小

To refine this answer by @JaredPar to be expanded and more performant:

function Get-DirectorySize() {
  param ([string]$root = $(Resolve-Path .))
  Get-ChildItem $root -Recurse -File |
    Measure-Object -Property Length -Sum |
    Select-Object -ExpandProperty Sum
}

Or, to make it more convenient for use explore type data:

Update-TypeData -TypeName System.IO.DirectoryInfo -MemberType ScriptProperty -MemberName Size -Value {
  Get-ChildItem $this -Recurse -File |
    Measure-Object -Property Length -Sum |
    Select-Object -ExpandProperty Sum
}

Then use by Get-ChildItem | Select-Object Name,Length,Size

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文