[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 896
  • Last Modified:

Folder Size with robocopy in powershell not working

Trying to develop a script to find folder size which has more than 256 character using robocopy in powershell. Getting the below error. Could you please help.

I got the script from

http://www.experts-exchange.com/Programming/Languages/Scripting/Powershell/Q_27890203.html

-------------------------------------------------------------script--------------------------------------------------------

$startFolder = "\\server\c$\accountlockout"
#Set output folder
$output = "C:\test\something.csv"
#Start the collections
$colItems = (Get-ChildItem $startFolder | Where-Object {$_.PSIsContainer -eq $True} | Sort-Object)
$results = @()
foreach ($i in $colItems)
    {          
        #START TIMER
        $startDTM = (Get-Date)
        $i.FullName
        $subFolderItems = (robocopy  $i.FullName "c:\Powershell" /zb /e /l /r:1 /w:1 /nfl /ndl /nc /fp /bytes /np /njh | ? {$_ -match "Bytes :"}).trim().split(" ")[2]
        #END TIMER
        $endDTM = (Get-Date)
        $results += '"' +$i.FullName + '"' + ";" + "{0:N2}" -f ($subFolderItems / 1MB) + ";" + "Elapsed Time: $(($endDTM-$startDTM).totalseconds) seconds"
    }
$results > $output
------------------------------------------------------------
error
\\server\c$\accountlockout\Hotel California (2013) 1CD Malayalam DVDRip x264 MP4
You cannot call a method on a null-valued expression.
At line:13 char:151
+         $subFolderItems = (robocopy  $i.FullName "c:\Powershell" /zb /e /l /r:1 /w:1 /nfl /ndl /nc /fp /bytes /np /njh | ? {$_ -match "Bytes :"}).trim <<<< ().split(" ")[2]
    + CategoryInfo          : InvalidOperation: (trim:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

Method invocation failed because [System.Object[]] doesn't contain a method named 'op_Division'.
At line:16 char:82
+         $results += '"' +$i.FullName + '"' + ";" + "{0:N2}" -f ($subFolderItems / <<<<  1MB) + ";" + "Elapsed Time: $(($endDTM-$startDTM).totalseconds) seconds"
    + CategoryInfo          : InvalidOperation: (op_Division:String) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound
0
Shani Basha
Asked:
Shani Basha
  • 9
  • 6
  • 2
  • +1
1 Solution
 
Shani BashaStorage Network SpecialistAuthor Commented:
Hi Ian,

Actually i am getting error while running the below piece of code

(robocopy "c:\program files" "c:\temp" /zb /e /l /r:1 /w:1 /nfl /ndl /nc /fp /bytes /np /njh | ? {
$_ -match "Bytes :"}).trim().split(" ")[2]


error is


\\server\c$\accountlockout\Hotel California (2013) 1CD Malayalam DVDRip x264 MP4
You cannot call a method on a null-valued expression.
At line:13 char:151
+         $subFolderItems = (robocopy  $i.FullName "c:\Powershell" /zb /e /l /r:1 /w:1 /nfl /ndl /nc /fp /bytes /np /njh | ? {$_ -match "Bytes :"}).trim <<<< ().split(" ")[2]
    + CategoryInfo          : InvalidOperation: (trim:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull
0
 
IanThCommented:
line 13 is

$subFolderItems = (robocopy  $i.FullName "c:\Powershell" /zb /e /l /r:1 /w:1 /nfl /ndl /nc /fp /bytes /np /njh | ? {$_ -match "Bytes :"}).trim().split(" ")[2]

I suspect a invalid argument check your code
0
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

 
Shani BashaStorage Network SpecialistAuthor Commented:
I couldn't find any error in the below line .could anyone please advice .Is  there anything with power shell version .

 $subFolderItems = (robocopy  $i.FullName "c:\Powershell" /zb /e /l /r:1 /w:1 /nfl /ndl /nc /fp /bytes /np /njh | ? {$_ -match "Bytes :"}).trim().split(" ")[2]
0
 
footechCommented:
You're receiving the first error "cannot call a method on a null-valued expression" because the command (robocopy  $i.FullName "c:\Powershell" /zb /e /l /r:1 /w:1 /nfl /ndl /nc /fp /bytes /np /njh | ? {$_ -match "Bytes :"}) hasn't returned anything.  Trim() is the method.

You could just try running robocopy  $i.FullName "c:\Powershell" /zb /e /l /r:1 /w:1 /nfl /ndl /nc /fp /bytes /np /njh substituting the particular path for $i.FullName to see what the result is, but I'm guessing that $i.FullName isn't a valid/existing path.

The second error is because $subFolderItems is an array.  How it got to be that way I don't know.
0
 
Shani BashaStorage Network SpecialistAuthor Commented:
ok . i have simplified the script to check that .getting the same error.

While removing all the robocopy  switches ,data is copying without any issues.means the paths are valid.

so  issue is in

$subFolderItems = (robocopy "\\server\c$\accountlockout\test" "c:\Powershell"  /zb /e /l /r:1 /w:1 /nfl /ndl /nc /fp /bytes /np /njh | ? {$_ -match "Bytes :"})

the code is not returning any values . is there anything else i need to add .

simplified script and error
--------------------------------------------------------------------------------------------------------------------------------
PS C:\WINDOWS\system32> #Set Start folder Location

$output = "\\server\c$\accountlockout\something.csv"

 $subFolderItems = (robocopy "\\server\c$\accountlockout\test" "c:\Powershell"  /zb /e /l /r:1 /w:1 /nfl /ndl /nc /fp /bytes /np /njh | ? {$_ -match "Bytes :"}).trim().split(" ")[2]
       
 $results += '"' +$i.FullName + '"' + ";" + "{0:N2}" -f ($subFolderItems )      
 
$results > $output
-------------------------------------------
Error
You cannot call a method on a null-valued expression.
At line:10 char:173
+   $subFolderItems = (robocopy "\\server\c$\accountlockout\test" "c:\Powershell"  /zb /e /l /r:1 /w:1 /nfl /ndl /nc /fp /bytes /np /njh | ? {$_ -match "Bytes :"}).trim <<<< ().split(" ")[2]
    + CategoryInfo          : InvalidOperation: (trim:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull
0
 
SubsunCommented:
What you get for
robocopy "c:\program files" "c:\temp" /zb /e /l /r:1 /w:1 /nfl /ndl /nc /fp /bytes /np /njh 

Open in new window

0
 
Shani BashaStorage Network SpecialistAuthor Commented:
i ran the script and is executing without any error.



out put csv
-----------------
C:\accountlockout\Hotel California (2013) 1CD Malayalam DVDRip x264 MP4;;Elapsed Time: 0.0990099 seconds
C:\accountlockout\test;;Elapsed Time: 0.0590059 seconds
C:\accountlockout\Up & Down (2013) 1CD Malayalam DVDRip x264 TEAM DDH~RG;;Elapsed Time: 0.0590059 seconds



 -------------
script
#Set Start folder Location
$startFolder = "C:\accountlockout"
#Set output folder
$output = "C:\accountlockout\something.csv"
#Start the collections
$colItems = (Get-ChildItem $startFolder | Where-Object {$_.PSIsContainer -eq $True} | Sort-Object)
$results = @()
foreach ($i in $colItems)
    {          
        #START TIMER
        $startDTM = (Get-Date)
        $i.FullName
        $subFolderItems = robocopy "c:\program files" "c:\temp" /zb /e /l /r:1 /w:1 /nfl /ndl /nc /fp /bytes /np /njh
        #END TIMER
        $endDTM = (Get-Date)
        $results += '"' +$i.FullName + '"' + ";" + "{0:N2}" -f  ($subFolderItems ) + ";" + "Elapsed Time: $(($endDTM-$startDTM).totalseconds) seconds"
    }
$results > $output


--------------------------------------
0
 
SubsunCommented:
So are you getting error when you run it against the file share? If yes then you need to check the access permissions for the share.. If you are able to run the robocopy command which I posted against the file share without any error then the script should give you the expected result..
0
 
Shani BashaStorage Network SpecialistAuthor Commented:
the thing is when i am adding  " | ? {$_ -match "Bytes :"}).trim().split(" ")[2]"   in the code
this is throwing error .

-------------------------------------------------------------------------------------------------------------------------------
PS C:\WINDOWS\system32> #Set Start folder Location
$startFolder = "C:\accountlockout"
#Set output folder
$output = "C:\accountlockout\something.csv"
#Start the collections
$colItems = (Get-ChildItem $startFolder | Where-Object {$_.PSIsContainer -eq $True} | Sort-Object)
$results = @()
foreach ($i in $colItems)
    {          
        #START TIMER
        $startDTM = (Get-Date)
        $i.FullName
        $subFolderItems = (robocopy "c:\program files" "c:\temp" /zb /e /l /r:1 /w:1 /nfl /ndl /nc /fp /bytes /np /njh | ? {$_ -match "Bytes :"}).trim().split(" ")[2]
        #END TIMER
        $endDTM = (Get-Date)
        $results += '"' +$i.FullName + '"' + ";" + "{0:N2}" -f  ($subFolderItems ) + ";" + "Elapsed Time: $(($endDTM-$startDTM).totalseconds) seconds"
    }
$results > $output
--------------------------------------
C:\accountlockout\Hotel California (2013) 1CD Malayalam DVDRip x264 MP4
You cannot call a method on a null-valued expression.
At line:13 char:151
+         $subFolderItems = (robocopy "c:\program files" "c:\temp" /zb /e /l /r:1 /w:1 /nfl /ndl /nc /fp /bytes /np /njh | ? {$_ -match "Bytes :"}).trim <<<< ().split(" ")[2]
    + CategoryInfo          : InvalidOperation: (trim:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull
 
C:\accountlockout\test
You cannot call a method on a null-valued expression.
At line:13 char:151
+         $subFolderItems = (robocopy "c:\program files" "c:\temp" /zb /e /l /r:1 /w:1 /nfl /ndl /nc /fp /bytes /np /njh | ? {$_ -match "Bytes :"}).trim <<<< ().split(" ")[2]
    + CategoryInfo          : InvalidOperation: (trim:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull
 
C:\accountlockout\Up & Down (2013) 1CD Malayalam DVDRip x264 TEAM DDH~RG
You cannot call a method on a null-valued expression.
At line:13 char:151
+         $subFolderItems = (robocopy "c:\program files" "c:\temp" /zb /e /l /r:1 /w:1 /nfl /ndl /nc /fp /bytes /np /njh | ? {$_ -match "Bytes :"}).trim <<<< ().split(" ")[2]
    + CategoryInfo          : InvalidOperation: (trim:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull
0
 
SubsunCommented:
I don't see any error with code..

In your code I can see, you are using static entry for source path robocopy "c:\program files" "c:\temp".......it will not give the corrct information..

Can you try the following code and see if the error exists?
$ErrorActionPreference = "Continue"
#Set Start folder Location
$startFolder = "C:\accountlockout"
#Set output folder
$output = "C:\accountlockout\something.csv"
#Start the collections
$colItems = (Get-ChildItem $startFolder | Where-Object {$_.PSIsContainer -eq $True} | Sort-Object)
$results = @()
foreach ($i in $colItems)
    {          
        #START TIMER
        $startDTM = (Get-Date) 
        $i.FullName
        $subFolderItems = ((robocopy $i.FullName "c:\temp" /zb /e /l /r:1 /w:1 /nfl /ndl /nc /fp /bytes /np /njh | ? {$_ -match "Bytes :"}).trim().split(" ") | ?{$_})[2]
        #END TIMER
        $endDTM = (Get-Date) 
        $results += '"' +$i.FullName + '"' + ";" + "{0:N2}" -f  ($subFolderItems) + ";" + "Elapsed Time: $(($endDTM-$startDTM).totalseconds) seconds"
    }
$results > $output

Open in new window

Also post the results of following commands....
robocopy "C:\accountlockout\Hotel California (2013) 1CD Malayalam DVDRip x264 MP4" "c:\temp" /zb /e /l /r:1 /w:1 /nfl /ndl /nc /fp /bytes /np /njh

robocopy "C:\accountlockout\Hotel California (2013) 1CD Malayalam DVDRip x264 MP4" "c:\temp" /zb /e /l /r:1 /w:1 /nfl /ndl /nc /fp /bytes /np /njh | ? {$_ -match "Bytes :"}

Open in new window

0
 
Shani BashaStorage Network SpecialistAuthor Commented:
Error still exists running the given code
--------------------------------------------------

PS C:\WINDOWS\system32> $ErrorActionPreference = "Continue"
#Set Start folder Location
$startFolder = "C:\accountlockout"
#Set output folder
$output = "C:\accountlockout\something.csv"
#Start the collections
$colItems = (Get-ChildItem $startFolder | Where-Object {$_.PSIsContainer -eq $True} | Sort-Object)
$results = @()
foreach ($i in $colItems)
    {          
        #START TIMER
        $startDTM = (Get-Date)
        $i.FullName
        $subFolderItems = ((robocopy $i.FullName "c:\temp" /zb /e /l /r:1 /w:1 /nfl /ndl /nc /fp /bytes /np /njh | ? {$_ -match "Bytes :"}).trim().split(" ") | ?{$_})[2]
        #END TIMER
        $endDTM = (Get-Date)
        $results += '"' +$i.FullName + '"' + ";" + "{0:N2}" -f  ($subFolderItems) + ";" + "Elapsed Time: $(($endDTM-$startDTM).totalseconds) seconds"
    }
$results > $output
C:\accountlockout\Hotel California (2013) 1CD Malayalam DVDRip x264 MP4
You cannot call a method on a null-valued expression.
At line:14 char:145
+         $subFolderItems = ((robocopy $i.FullName "c:\temp" /zb /e /l /r:1 /w:1 /nfl /ndl /nc /fp /bytes /np /njh | ? {$_ -match "Bytes :"}).trim <<<< ().split(" ") | ?{$_})[2]
    + CategoryInfo          : InvalidOperation: (trim:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull
 --------------------------------------------------------------------------------------------------------------------------------
running with

robocopy "C:\accountlockout\Hotel California (2013) 1CD Malayalam DVDRip x264 MP4" "c:\temp" /zb /e /l /r:1 /w:1 /nfl /ndl /nc /fp /bytes /np /njh

not getting any error .


PS C:\WINDOWS\system32> $ErrorActionPreference = "Continue"
#Set Start folder Location
$startFolder = "C:\accountlockout"
#Set output folder
$output = "C:\accountlockout\something.csv"
#Start the collections
$colItems = (Get-ChildItem $startFolder | Where-Object {$_.PSIsContainer -eq $True} | Sort-Object)
$results = @()
foreach ($i in $colItems)
    {          
        #START TIMER
        $startDTM = (Get-Date)
        $i.FullName
        $subFolderItems = robocopy "C:\accountlockout\Hotel California (2013) 1CD Malayalam DVDRip x264 MP4" "c:\temp" /zb /e /l /r:1 /w:1 /nfl /ndl /nc /fp /bytes /np /njh
        #END TIMER
        $endDTM = (Get-Date)
        $results += '"' +$i.FullName + '"' + ";" + "{0:N2}" -f  ($subFolderItems) + ";" + "Elapsed Time: $(($endDTM-$startDTM).totalseconds) seconds"
    }
$results > $output

C:\accountlockout\Hotel California (2013) 1CD Malayalam DVDRip x264 MP4
C:\accountlockout\test
C:\accountlockout\Up & Down (2013) 1CD Malayalam DVDRip x264 TEAM DDH~RG
-------------------------------------------------------------------------------------------------------------------------------
running with

robocopy "C:\accountlockout\Hotel California (2013) 1CD Malayalam DVDRip x264 MP4" "c:\temp" /zb /e /l /r:1 /w:1 /nfl /ndl /nc /fp /bytes /np /njh | ? {$_ -match "Bytes :"}

not getting any error . but the size in output is zero



not getting any error
PS C:\WINDOWS\system32> $ErrorActionPreference = "Continue"
#Set Start folder Location
$startFolder = "C:\accountlockout"
#Set output folder
$output = "C:\accountlockout\something.csv"
#Start the collections
$colItems = (Get-ChildItem $startFolder | Where-Object {$_.PSIsContainer -eq $True} | Sort-Object)
$results = @()
foreach ($i in $colItems)
    {          
        #START TIMER
        $startDTM = (Get-Date)
        $i.FullName
        $subFolderItems = robocopy "C:\accountlockout\Hotel California (2013) 1CD Malayalam DVDRip x264 MP4" "c:\temp" /zb /e /l /r:1 /w:1 /nfl /ndl /nc /fp /bytes /np /njh | ? {$_ -match "Bytes :"}
        #END TIMER
        $endDTM = (Get-Date)
        $results += '"' +$i.FullName + '"' + ";" + "{0:N2}" -f   ($subFolderItems / 1MB) + ";" + "Elapsed Time: $(($endDTM-$startDTM).totalseconds) seconds"
    }
$results > $output
---------
C:\accountlockout\Hotel California (2013) 1CD Malayalam DVDRip x264 MP4
C:\accountlockout\test
C:\accountlockout\Up & Down (2013) 1CD Malayalam DVDRip x264 TEAM DDH~RG

output csv

C:\accountlockout\Hotel California (2013) 1CD Malayalam DVDRip x264 MP4;0.00;Elapsed Time: 0.0790079 seconds
C:\accountlockout\test;0.00;Elapsed Time: 0.0370037 seconds
C:\accountlockout\Up & Down (2013) 1CD Malayalam DVDRip x264 TEAM DDH~RG;0.00;Elapsed Time: 0.0350035 seconds
0
 
SubsunCommented:
Sorry.. I am not able to recreate the issue.. The code works for me without any error.. Can you post the output of the robocopy commands which I posted #a39677403??

Also are you testing with empty folders?
0
 
footechCommented:
Agreed it's important to post the output of running the command even if there is no error.  This is to see the output that you're getting and determine whether the Where-Object filter is filtering all the output (i.e. is not getting a match).
0
 
Shani BashaStorage Network SpecialistAuthor Commented:
I installed powershell in another machine and now the script worked!

The below script is working fine to get the folder size of "\\servername\21EM0032"

Could you please help me to get the input (shared folders) from a notepad and execute

------------------------------------------------------------------------------------------------------------------------------

$ErrorActionPreference = "Continue"

#Set output folder
$output = "F:\Backup Nov 20 2013\new.csv"

     
       
        $startDTM = (Get-Date)
     
       
        $subFolderItems = ((robocopy   "\\servername\21EM0032" "c:\temp" /zb /e /l /r:1 /w:1 /nfl /ndl /nc /fp /bytes /np /njh | ? {$_ -match "Bytes :"}).trim().split(" ") | ?{$_})[2]
        #END TIMER
        $endDTM = (Get-Date)
       
        $results +=  "{0:N2}" -f  (($subFolderItems / 1MB)/1024)
   
$results > $output
0
 
SubsunCommented:
Somehow, I still don't think it's a PowerShell issue.. It's probably a permission issue..
Try this to read the input directories from a text file..
#Set Intput file
$Inputfile = "C:\Temp\input.txt"
#Set output folder
$output = "F:\Backup Nov 20 2013\new.csv"
$ErrorActionPreference = "Continue"
$results = @()
Get-Content $Inputfile | % {
 $path = $_
 $startDTM = (Get-Date)
 $subFolderItems = ((robocopy $path "c:\temp" /zb /e /l /r:1 /w:1 /nfl /ndl /nc /fp /bytes /np /njh | ? {$_ -match "Bytes :"}).trim().split(" ") | ?{$_})[2]
 #END TIMER
 $endDTM = (Get-Date)
 $results +=  New-Object PSObject -Property @{
 Folder = $path
 Size = "{0:N2}" -f  (($subFolderItems / 1MB)/1024)
 }
}
$results | Export-Csv $output -NTI

Open in new window

0
 
Shani BashaStorage Network SpecialistAuthor Commented:
Hi  Subsun

perfect. thank you

Is it possible to add code for extracting  share path details of a windows server to input.txt  and then execute

Also i need share name in the csv output along with share path and size .
0
 
SubsunCommented:
You can run the following code to export the server share details to csv file..
Get-WmiObject Win32_Share | ?{$_.Type -eq 0} | Select Name,Path | Export-Csv C:\report.csv

Open in new window

To use the csv file in your script..
#Set Intput file
$Inputfile = "C:\Temp\input.csv"
#Set output folder
$output = "F:\Backup Nov 20 2013\new.csv"
$ErrorActionPreference = "Continue"
$results = @()
Import-Csv $Inputfile | % {
 $path = $_.Path
 $startDTM = (Get-Date)
 $subFolderItems = ((robocopy $path "c:\temp" /zb /e /l /r:1 /w:1 /nfl /ndl /nc /fp /bytes /np /njh | ? {$_ -match "Bytes :"}).trim().split(" ") | ?{$_})[2]
 #END TIMER
 $endDTM = (Get-Date)
 $results +=  New-Object PSObject -Property @{
 Folder = $path
 Size = "{0:N2}" -f  (($subFolderItems / 1MB)/1024)
 }
}
$results | Export-Csv $output -NTI

Open in new window

In future, please open a new question for any additional requests. Thanks..
0
 
Shani BashaStorage Network SpecialistAuthor Commented:
Thanks a lot  Subsun . some additional info needed .opened a new post

http://www.experts-exchange.com/Programming/Languages/Scripting/Powershell/Q_28304534.html
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

  • 9
  • 6
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now