首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >用于获取目录总大小的PowerShell脚本

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

Stack Overflow用户
提问于 2009-05-01 04:53:29
回答 4查看 20.3K关注 0票数 18

我需要递归地获取目录的大小。我每个月都要这样做,所以我想做一个PowerShell脚本来做它。

我该怎么做呢?

EN

回答 4

Stack Overflow用户

发布于 2009-05-01 17:34:26

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

票数 3
EN

Stack Overflow用户

发布于 2012-05-14 22:42:38

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

代码语言:javascript
复制
(gci d:\folder1 -r -force -include *.txt,*.csv | measure -sum -property Length).Sum
票数 3
EN

Stack Overflow用户

发布于 2012-11-28 07:37:37

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

代码语言:javascript
复制
# 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
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/809014

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档