RoboCopy目录使用PowerShell调整大小
问题描述:
我正试图获取仅在我的根文件夹中的子目录的报告。尝试这样做时,我收到两个错误。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\
答
的问题是,你需要在$colitems
使用$DIR.fullname
为每一个元素都是DirectoryInfo
类型的,你不能把它作为一个字符串作为你的代码ROBOCOPY后再行做创建你的结果。
这是一个固定的版本
function get_dir ($SDIR)
{
$colItems = Get-ChildItem $SDIR -Directory
foreach ($DIR in $colItems)
{
$fldSize = (robocopy $DIR.fullname "NO" /e /l /r:0 /w:0 /nfl /ndl /nc /fp /np /njh /xj /bytes| ? {$_ -match "Bytes :"}).trim().split(" ")[2]
$DIR.fullname + " = " + ("{0:N2}" -f ($fldSize/1MB) + " MB")
}
}
感谢您的迅速反应。我知道我错过了一些东西,你的解释帮助我不仅明白我错过了什么,而且为什么这是必要的。 – GreatMagusKyros