Calendar

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

View posts in large calendar

RecentComments

None

 
 
     
 

This translet will add a Ratio attribute to a daxml file. It is very useful to find out where all of the space is taken up in folders and if your downstream application is SVG or WPF then Ratio can be used in a lot of places.

function T-AddRatio{
    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|file">
       
<xsl:copy>
           
<xsl:if test="parent::*/@Length">
               
<xsl:attribute name="Ratio">
                   
<xsl:value-of select="@Length div parent::*/@Length" />
               
</xsl:attribute>
           
</xsl:if>
           
<xsl:if test="@Base">
               
<xsl:attribute name="Ratio">1</xsl:attribute>
           
</xsl:if>
           
<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)
        }
    }
}

As you can see in the code it checks that the folder node has a @Length attribute. Length property is not added to folders by Get-DirAsXml so first use the T-DirLength translet to add a @Length attribute.

It can be used like this

PS> . .\Get-DirAsXml
PS> . .\T-DirLength
PS> . .\T-AddRatio
PS> Get-DirAsXml .\test -props @{Length=""}|T-DirLength|T-AddRatio

It might produce this

<root Name="root" Root="True" Date="2008/12/03 11:15:51">
   
<folder Ratio="1" Length="19678" Name="test" Base="D:\powershell\blog\test" Parent="D:\powershell\blog">
       
<folder Ratio="0.5" Length="9839" Name="test2">
           
<file Ratio="0.3384490293729038" Name="test.ps1" Length="3330" />
           
<file Ratio="0.08496798455127553" Name="test.txt" Length="836" />
           
<file Ratio="0.1311108852525663" Name="test.zip" Length="1290" />
           
<file Ratio="0.4454721008232544" Name="tmp.xml" Length="4383" />
       
</folder>
       
<file Ratio="0.1692245146864519" Name="test.ps1" Length="3330" />
       
<file Ratio="0.04248399227563777" Name="test.txt" Length="836" />
       
<file Ratio="0.06555544262628315" Name="test.zip" Length="1290" />
       
<file Ratio="0.2227360504116272" Name="tmp.xml" Length="4383" />
   
</folder>
</root>

Here is the code

T-AddRatio.zip (671 b)

Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

Add comment