My digital notepad RSS 2.0
 Friday, January 05, 2007

A somewhat akward title, but I had a hard time finding a better one :)

We have a projects folder. In that folder there is one folder for each project and in each project folder there is a documents folder (among many others). Now, I needed to find the size of each documents folder for each project and ignore the other folders (don't ask me why).

Seeing as there are a few hundred projects and each documents folder can contain from five to several thousand files I did not want to check each folder. Since the other folders are even larger, listing the size of everything and take out what I did not need would increase the time to create the listing.

So I created a PowerShell script. Was that the fastest way to do it? Probably not, but I'll have to do this again someday and then I'll save lots of time. Besides, it didn't take all that long to create.

$hashTbl=@{};
Get-ChildItem | where-object {$_.Mode -eq "d----"} | `
 foreach-object  { `
   $_.Name;`
  $hashTbl[$_] = ( get-childitem($_.Name + "\documents")`
-recurse -force| `
   measure-object length -sum )
  };
$output = ($hashTbl.psbase.keys |`
 foreach `
  {"Name,Size,File count"} `
  { $_.Name + "," + $hashTbl[$_].Sum + ","`
+ $hashTbl[$_].Count } `
 );
New-Item c:\temp\DirectorySize.csv -type file -force | out-null;
Add-Content c:\temp\DirectorySize.csv  $output;
import-csv c:\temp\DirectorySize.csv;

Since some might wonder what the heck I'm doing here (like myself, six months from now) I thought I'd go through it line by line.

Instansiate an empty hash table

$hashTbl=@{};

Get a list of all children in the current directory and use where-object to get only the objects that have the directory attibute i.e. just directories and not files.

Get-ChildItem | where-object {$_.Mode -eq "d----"} | `

The result from here is piped into a foreach-object that will do an operation for each object.

 foreach-object  { `

In this operation I display the Name of the folder. This will be outputted while the script is running, giving me feedback on progress. The ; seperates commands.

   $_.Name;`

The next command might need a bit more explaining.

  $hashTbl[$_] = ( get-childitem($_.Name + "\documents")`
-recurse -force| `
   measure-object length -sum )

From the inside and out: First we use get-childitem for the documents folder. $_ is an auto-variable from the foreach and contains the current object in the loop. $_.Name is the name of that folder. Add "\documents" to that to look at the sub-folder I want. -recurse gets the children of all sub folders and -force includes hidden files.

get-childitem($_.Name + "\documents") -recurse -force 

Then we pipe that into measure-object and tells it to sum the length property of all the items returned from get-childitem.

get-childitem($_.Name + "\documents") -recurse -force| `
   measure-object length -sum )

That creates a GenericMeasureInfo object that I put into the hashtable for later.

$hashTbl[$_] = ( get-childitem($_.Name + "\documents")`
-recurse -force| `
   measure-object length -sum )

This loops though all the keys in the hashtable and outputs a string with the values comma-separated. The {"Name,Size,File count"} is the before statement in the foreach that executes before the loop, outputting the headers for my comma-separated list. The ( ) around everything makes it a list that I can save in $output.

$output = ($hashTbl.psbase.keys |`
 foreach `
  {"Name,Size,File count"} `
  { $_.Name + "," + $hashTbl[$_].Sum + ","`
+ $hashTbl[$_].Count } `
 );

Create a new file, overwriting any old file. Output is supressed.

New-Item c:\temp\DirectorySize.csv -type file -force | out-null;

Add the comma-separated list to the file.

Add-Content c:\temp\DirectorySize.csv  $output; 

Read it and display it for information purposes only.

import-csv c:\temp\DirectorySize.csv;

There, all done. Any questions?

subSubDirSize.ps1 (,51 KB)
 
Friday, January 05, 2007 3:00:02 PM (Central Europe Standard Time, UTC+01:00)  #    Comments [3] -
Administration | Powershell | Scripting
Links
Twitter updates
    Archive
    <March 2010>
    SunMonTueWedThuFriSat
    28123456
    78910111213
    14151617181920
    21222324252627
    28293031123
    45678910
    About the author/Disclaimer

    Disclaimer
    The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

    © Copyright 2010
    Glenn F. Henriksen
    Sign In
    Statistics
    Total Posts: 48
    This Year: 0
    This Month: 0
    This Week: 0
    Comments: 31
    All Content © 2010, Glenn F. Henriksen
    DasBlog theme 'Business' created by Christoph De Baene (delarou)