0
我正試圖獲取僅在我的根文件夾中的子目錄的報告。嘗試這樣做時,我收到兩個錯誤。RoboCopy目錄使用PowerShell調整大小
You cannot call a method on a null-valued expression.
At line:5 char:1
+ $fldSize = (robocopy $DIR "NO" /e /l /r:0 /w:0 /nfl /ndl /nc /fp /np ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
Method invocation failed because [System.IO.DirectoryInfo] does not contain a method named 'op_Addition'.
At line:6 char:1
+ $DIR + " = " + "{0:N2}" -f ($fldSize/1MB) + " MB"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (op_Addition:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
我已經做了一點看,什麼我發現似乎表明一個數組的問題,但是沒有什麼迄今爲止我曾嘗試已取得任何不同的結果。腳本在下面。
function get_dir ($SDIR){
$colItems = Get-ChildItem $SDIR -Directory
foreach ($DIR in $colItems)
{
$fldSize = (robocopy $DIR "NO" /e /l /r:0 /w:0 /nfl /ndl /nc /fp /np /njh /xj /bytes| ? {$_ -match "Bytes :"}).trim().split(" ")[2]
$DIR + " = " + "{0:N2}" -f ($fldSize/1MB) + " MB"
}
}
get_dir C:\TEST\
感謝您的迅速反應。我知道我錯過了一些東西,你的解釋幫助我不僅明白我錯過了什麼,而且爲什麼這是必要的。 – GreatMagusKyros