[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Folder Size with robocopy in powershell not working

Posted on 2013-11-26
19
Medium Priority
?
908 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:SHB
  • 9
  • 6
  • 2
  • +1
19 Comments
 
LVL 10

Author Comment

by:SHB
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
Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

 
LVL 10

Author Comment

by:SHB
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 41

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 10

Author Comment

by:SHB
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 10

Author Comment

by:SHB
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
 
LVL 10

Author Comment

by:SHB
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 10

Author Comment

by:SHB
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 41

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 10

Author Comment

by:SHB
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 10

Author Comment

by:SHB
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 2000 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 10

Author Comment

by:SHB
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

Evaluating UTMs? Here's what you need to know!

Evaluating a UTM appliance and vendor can prove to be an overwhelming exercise.  How can you make sure that you're getting the security that your organization needs without breaking the bank? Check out our UTM Buyer's Guide for more information on what you should be looking for!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Screencast - Getting to Know the Pipeline
Suggested Courses

611 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