I am attempting to get a report of only the sub-directories in my root folder. I am receiving two errors when attempting to do this.
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
I've done a bit of looking, and what I have found seems to indicate an array problem, however nothing I have tried thus far has yielded any different results. Script is below.
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\
The issue is that you need to use $DIR.fullname
as each element in $colitems
is of type DirectoryInfo
and you cannot treat it as a string to create your result as you do in the line of code after the robocopy.
Here is a fixed version
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")
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.