Calendar

<<  August 2017  >>
MonTueWedThuFriSatSun
31123456
78910111213
14151617181920
21222324252627
28293031123
45678910

View posts in large calendar

RecentComments

None

 
 
     
 

These are small Xslt transforms that modify some Xml in the powershell pipeline. Here is a simple one. It adds a Length attribute to daxml folder nodes.

function T-DirLength{
    param ($inxml)
    begin{
        . PSlib:\xml\invoke-transform.ps1
        [xml]$xslt = @"
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
   
<xsl:template match="node()|@*">
       
<xsl:copy>
           
<xsl:apply-templates select="@*|node()" />
       
</xsl:copy>
   
</xsl:template>
   
<xsl:template match="folder">
       
<xsl:copy>
           
<xsl:attribute name="Length">
               
<xsl:value-of select="sum(.//file/@Length)" />
           
</xsl:attribute>
           
<xsl:apply-templates select="@*|node()" />
       
</xsl:copy>
   
</xsl:template>
</xsl:stylesheet>
"@
    process{
        if ($_ -is [xml]){
            [xml](invoke-transform -inxml $_ -inxsl $xslt)
        }
    }
    end{
        if ($inxml -is [xml]){
           [xml](invoke-transform -inxml $inxml -inxsl $xslt)
        }
    }
}

It does an Xslt identity transform on the Xml except for the 'folder' nodes to which it adds a Length attribute which is the sum of all the file/@Length attributes below the folder. Folders do not have a Length property so do not get a Length attribute even if you specify -props @{Length=""}. You could write a get-dirasxml custom props script to do this but getting that value at shell level is slow. This is much faster.

It can be used like this

PS> . .\T-DirLength.ps1 #dot source the translet file
PS> . .\Get-DirAsXml.ps1 #dot source the Get-DirAsXml file
PS> Get-DirAsXml D:\powershell\test -props @{Length=""} | T-DirLength

and might produce

<root Name="root" Root="True" Date="2008/11/03 09:55:40">
   
<folder Length="9839" Name="test" Base="D:\powershell\test">
       
<file Name="test.txt" Length="836" />
       
<file Name="test.ps1" Length="3330" />
       
<file Name="test.zip" Length="1290" />
       
<file Name="tmp.xml" Length="4383" />
   
</folder>
</root>

Here is the code

T-DirLength.zip (635 b)

Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

Add comment