PowerShell获取指定目录下文件列表和大小并保存成txt文档

来源:互联网 发布:项目管理系统 php svn 编辑:程序博客网 时间:2024/04/28 21:39
#列出filepath下所有子文件夹并统计子文件夹大小function filesize ([string]$filepath){    if ($filepath -eq $null)    {        throw "路径不能为空"    }    $_.name + "文件夹大小(MB)" -f $l | Out-File ($filepath+"test.txt")    dir -Path $filepath |    ForEach-Object -Process {        if ($_.psiscontainer -eq $true)        {#文件夹大小            $length = 0            dir -Path $_.fullname -Recurse | ForEach-Object{                $length += $_.Length            }            $l = $length/1MB            # 输出在控制台            $_.name + "文件夹的大小为: {0:n2} MB" -f $l            # 写入TXT文件            $_.name + "{0:n2}" -f $l | Out-File -Append ($filepath+"test.txt")        }else        {#文件大小            $length = 0            dir -Path $_.fullname -Recurse | ForEach-Object{                $length += $_.Length            }            $l = $length/1MB            $_.name + "文件的大小为: {0:n2} MB" -f $l            $_.name + "{0:n2}" -f $l | Out-File -Append ($filepath+"test.txt")        }    }}filesize -filepath "C:\pythontest\"


下载链接:http://download.csdn.net/detail/u013252072/9716435

0 0
原创粉丝点击