Solved

Folder Size with robocopy in powershell not working

Posted on 2013-11-26
19
833 Views
Last Modified: 2013-11-26
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
Comment
Question by:Shani Basha
  • 9
  • 6
  • 2
  • +1
19 Comments
 
LVL 30

Expert Comment

by:IanTh
ID: 39676907
0
 
LVL 9

Author Comment

by:Shani Basha
ID: 39676919
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
 
LVL 30

Expert Comment

by:IanTh
ID: 39676923
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
 
LVL 9

Author Comment

by:Shani Basha
ID: 39676947
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
 
LVL 39

Expert Comment

by:footech
ID: 39677014
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
 
LVL 9

Author Comment

by:Shani Basha
ID: 39677212
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
 
LVL 40

Expert Comment

by:Subsun
ID: 39677217
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
 
LVL 9

Author Comment

by:Shani Basha
ID: 39677289
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
 
LVL 40

Expert Comment

by:Subsun
ID: 39677303
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 9

Author Comment

by:Shani Basha
ID: 39677350
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
 
LVL 40

Expert Comment

by:Subsun
ID: 39677403
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
 
LVL 9

Author Comment

by:Shani Basha
ID: 39677433
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
 
LVL 40

Expert Comment

by:Subsun
ID: 39677449
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
 
LVL 39

Expert Comment

by:footech
ID: 39678091
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
 
LVL 9

Author Comment

by:Shani Basha
ID: 39678166
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
 
LVL 40

Expert Comment

by:Subsun
ID: 39678375
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
 
LVL 9

Author Comment

by:Shani Basha
ID: 39678698
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
 
LVL 40

Accepted Solution

by:
Subsun earned 500 total points
ID: 39678806
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
 
LVL 9

Author Comment

by:Shani Basha
ID: 39678896
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

This article shows how a content item can be identified directly or through translation of a navigation type. It then shows how this information can be used to create a menu for further navigation.
Set OWA language and time zone in Exchange for individuals, all users or per database.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now