【问题标题】:RoboCopy Directory Sizing with PowerShell使用 PowerShell 调整 RoboCopy 目录大小
【发布时间】:2017-11-01 19:54:02
【问题描述】:

我正在尝试仅获取根文件夹中子目录的报告。尝试执行此操作时收到两个错误。

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\

【问题讨论】:

    标签: powershell directory size subdirectory robocopy


    【解决方案1】:

    问题是您需要使用$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")
        }
    }
    

    【讨论】:

    • 感谢您的快速回复。我知道我遗漏了一些东西,您的解释不仅帮助我理解了我遗漏了什么,而且帮助我理解了为什么它是必要的。
    猜你喜欢
    • 2010-11-06
    • 1970-01-01
    • 2014-08-22
    • 1970-01-01
    • 2017-09-24
    • 2016-02-12
    • 2014-07-14
    • 2021-09-20
    • 1970-01-01
    相关资源
    最近更新 更多