2017-04-11 285 views
0

指定されたフォルダのフォルダサイズを報告できるスクリプトを作成しようとしています。get-childitem:存在しないためパス ''が見つかりません

フォルダ名を入力してもこのエラーが発生していますが、フォルダが存在することがわかりました。

また、このエラーが発生しない場合、私は常に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

問題はPowerShellセッションの実行アーキテクチャであり、そのフォルダがどこにあるかを示します。 c:\ windows \ syswow64 \ downloadsは何かを返しますか? – Matt

答えて

-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 
+0

あなたのコードは、すべてのファイルの合計サイズを計算することに関して改善されていますが、OPの症状(現在の場所や誤ったパスに関する誤った仮定から来た可能性が高い)を説明するものではなく、 $ startfolder'は_stringですが、存在しない '.FullName'プロパティにアクセスしようとしています。この点でOPのコードは正しいです。 – mklement0

関連する問題