我正在尝试创建一个可以报告指定文件夹的文件夹大小的脚本。
我输入文件夹名称后仍然收到此错误,我知道该文件夹存在。
此外,如果我没有收到此错误,那么我总是得到0 MB。
get-childitem : Cannot find path 'C:\WINDOWS\system32\downloads' because it does not exist.
At C:\Users\Erik\Desktop\powershell script.ps1:58 char:12
+ $folders = get-childitem $startfolder | where{$_.PSiscontainer -eq "T ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:\WINDOWS\system32\downloads:String) [Get-ChildItem], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound, Microsoft.PowerShell.Commands.GetChildItemCommand
代码
$startfolder = Read-Host -Prompt 'Please enter the folder name:'
#check that input is not empty
if([string]::IsNullOrEmpty($startfolder)) {
Write-Host "folder name is NULL or EMPTY"
} else {
$folders = get-childitem $startfolder | where {$_.PSiscontainer -eq "True"}
"folder Name`tfolder Size (MB)"
foreach ($fol in $Folders) {
$colItems = (Get-ChildItem $fol.fullname -recurse | Measure-Object -property length -sum)
$size = "{0:N2}" -f ($colItems.sum / 1MB)
"$($fol.name)`t$size"
}
}
答案 0 :(得分:-3)
所以..我的电源壳有点可怕,但我已经为我工作了这个:
$startfolder = Read-Host -Prompt 'Please enter the folder name:'
#check that input is not empty
if([string]::IsNullOrEmpty($startfolder)) {
Write-Host "folder name is NULL or EMPTY"
}
else {
$subFolderItems = Get-ChildItem $startfolder.FullName -recurse -force | Where-Object {$_.PSIsContainer -eq $false} | Measure-Object -property Length -sum | Select-Object Sum
$startfolder + " -- " + "{0:N2}" -f ($subFolderItems.sum / 1MB) + " MB"
}
输出如下:
test -- 2.65 MB