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

#列出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

猜你喜欢

转载自blog.csdn.net/u013252072/article/details/53762105
今日推荐