使用 PowerShell 调整 RoboCopy 目录大小
RoboCopy Directory Sizing with 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\
问题是您需要使用 $DIR.fullname
,因为 $colitems
中的每个元素都是 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")
}
}
我正在尝试仅获取根文件夹中子目录的报告。我在尝试执行此操作时收到两个错误。
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\
问题是您需要使用 $DIR.fullname
,因为 $colitems
中的每个元素都是 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")
}
}