您的位置:首页 > 运维架构 > Shell

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

2016-12-20 13:49 756 查看
#列出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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐