PowerShell獲取指定目錄下檔案列表和大小並儲存成txt文件
阿新 • • 發佈:2019-01-28
#列出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\"