Calendar

<<  July 2017  >>
MonTueWedThuFriSatSun
262728293012
3456789
10111213141516
17181920212223
24252627282930
31123456

View posts in large calendar

RecentComments

None

 
 
     
 

Displaying the output of Get-DirAsXml in an explorer with Powershell.  Although Powershell is a command line interface it can use forms to display output and get input. For an explorer the first thing we need is a treeview so we can use the System.Windows.Forms.TreeView control. Add it to a System.Windows.Forms.Form then recursively wander down the xml tree adding TreeNodes to it for all the nodes in the xml. Here is some code that will do that.

function Form-DisplayDir{
    param ([xml]$XML)
    function DisplayDir_wander($el, $tnode){
        foreach($e in $el.get_ChildNodes()){ #|sort-object name){
            $tn = new-object System.Windows.Forms.TreeNode
            $tn.Text = $e.name
            [Void]$tnode.Nodes.add( $tn )
            if ($e.get_Name() -eq "folder"){
                DisplayDir_wander ($e) ($tn)
            }
        }
    }

    $FORM = new-object Windows.Forms.Form   
    $FORM.Size = new-object System.Drawing.Size(300,310)   
    $FORM.text = "Form-DisplayDir"
   
    $RESULT = ""
    $TREEVIEW = new-object windows.forms.TreeView 
    $TREEVIEW.size = new-object System.Drawing.Size(295,269)  
    $TREEVIEW.Anchor = "top, left, bottom, right"
    $TREEVIEW.add_AfterSelect({
        $RESULT = $TREEVIEW.SelectedNode.FullPath
    })
       
    $r = $XML.selectSingleNode("/*")
    $tn = new-object System.Windows.Forms.TreeNode 
    $tn.Text = $r.get_Name()
               
    DisplayDir_wander ($r) ($tn)
       
    [void]$TREEVIEW.Nodes.Add($tn)
    $FORM.Controls.Add($TREEVIEW)
    [void]$FORM.showdialog()
    $RESULT
}

You can use it like this

PS> . .\Form-DisplayDir.ps1 # dot source the script
PS> Form-DisplayDir (Get-DirAsXml test)

It might be displayed like this.

Form-DisplayDir1 

PS> Form-DisplayDir (Get-DirAsXml test -props @{Length=""})

Will display this form with the file sizes in bytes shown.

Form-DisplayDir2 

The next thing to do is add an event handler to the TreeView. I like to use the AfterSelect event rather than the Click event because it doesn't fire when you expand or collapse nodes.

$TREEVIEW.add_AfterSelect({
   
$RESULT = $TREEVIEW.SelectedNode.FullPath
})

This bit of code can then be used as a simple folder/file selector. You could add the event to a Button and just stick $RESULT into the pipeline after the form closes.

PS> Form-DisplayDir (Get-DirAsXml test)
root\test\test2\test.txt

or
PS> Form-DisplayDir (Get-DirAsXml test) | SomeScriptThatNeedsAFilePath.ps1

Here is the code

Form-DisplayDir.zip (621.00 bytes)

The next thing to do is add a System.Windows.Forms.ListView control to display the files.

   
    $LISTVIEW = new-object windows.forms.ListView
    $LISTVIEW.Location = new-object System.Drawing.Size(300, 0)
    $LISTVIEW.Size = new-object System.Drawing.Size(295,269)
    $LISTVIEW.Anchor = "top, left, bottom, right"
    $LISTVIEW.View = [System.Windows.Forms.View]::Details
    $LISTVIEW.AllowColumnReorder = $true
    [void]$LISTVIEW.Columns.Add("Name", -2, [windows.forms.HorizontalAlignment]::left)
    foreach($att in $XML.SelectSingleNode("//file"
            ).SelectNodes("@*[not(name()='Name')]")){
        [void]$LISTVIEW.Columns.Add($att.get_Name(), -2, [windows.forms.HorizontalAlignment]::Right)
    }
    $TREEVIEW.add_AfterSelect({
        $LISTVIEW.Items.Clear()
        $xmlnode = $TREEVIEW.SelectedNode.Tag
        foreach($child in $xmlnode.get_ChildNodes()){
            $item = new-object windows.forms.ListViewItem($child.Name)
            foreach($column in ($LISTVIEW.Columns|where{$_.Text -ne "Name"})){
                if ($child.($column.Text) -ne $null){
                    $item.SubItems.Add($child.($column.Text))
                }
            }
            $LISTVIEW.Items.Add($item)
        }
    })
    $FORM.Controls.Add($LISTVIEW)

The TreeView AfterSelect event has been changed to populate the ListView.

To make it look a bit more like Explorer you probably want to add a couple more columns to the xml

PS> Form-DisplayDirExp (Get-DirAsXml test -props @{Length="";LastWriteTime=""})

It might be displayed like this

 FormDisplayDirExp12

Here is the code

Form-DisplayDirExp.zip (953.00 bytes)

It is getting there but it is a bit confusing without icons, especially for the folders. There are a few different ways of doing this. You could create a System.Windows.Forms.ImageList object and add images to that then assign it to the $TREEVIEW.ImageList property but it gets quite labour intensive to make a lot of little icons. You could have just 2 icons, one for folders and one for files. So add some code like this

$ImageList = New-Object System.Windows.Forms.ImageList 
$imageList.Images.Add([System.Drawing.Bitmap]::FromFile("folder.bmp"))
$imageList.Images.Add([System.Drawing.Bitmap]::FromFile("file.bmp"))
$TREEVIEW.ImageList = $ImageList 
$LISTVIEW.SmallImageList = $ImageList 

It might be displayed like this

Form-DisplayDirExp2

 

The best way to add icons is to use the SystemImageList or Shell Icon Cache. It is used everywhere and if you are using TreeView and ListView controls then one would think there would be a simple way of saying Hey! TreeView."UseTheEffingSystemImageList" But it is not that simple. You have to get down to the nitty gritty with System.Runtime.InteropServices SHGetFileInfo and SendMessage. So I wrapped all that 'c++ twitter' up in a file that you can just 'dot source' and then use the 2 methods (SetSystemImageListHandle, GetSystemImageListIndex) it contains.

int SetSystemImageListHandle(Control, int Size) sets the ImageList for the TreeView/ListView control to the SystemImageList and the Size to large or small (0, 1).

int GetSystemImageListIndex(string Filename, boolean Dir, int Size) gets the image index for the filename. Dir is a boolean indicating if it is a Folder and Size is large or small (0, 1).

ImageListHandle.zip (2.19 kb)

Here are some code snippets.

. .\ImageListHandle.ps1
$SHGFI_SMALLICON = 1; $SHGFI_LARGEICON = 0
$LISTVIEW = new-object System.Windows.Forms.ListView
$FORM.Controls.Add($LISTVIEW)
[void][cjb.Shell32]::SetSystemImageListHandle($LISTVIEW, $SHGFI_SMALLICON)
$TREEVIEW = new-object System.Windows.Forms.TreeView
$FORM.Controls.Add($TREEVIEW)
[void][cjb.Shell32]::SetSystemImageListHandle($TREEVIEW, $SHGFI_SMALLICON)
$tn = new-object System.Windows.Forms.TreeNode
$tn.ImageIndex = $tn.SelectedImageIndex = [cjb.Shell32]::GetSystemImageListIndex($e.Name, ($e.get_Name() -eq "folder"), $SHGFI_SMALLICON)
$idx = [cjb.Shell32]::GetSystemImageListIndex($n.Name, ($n.get_Name() -eq "folder"), $SHGFI_SMALLICON)
$item = new-object windows.forms.ListViewItem($n.Name,$idx)

For this to work properly the control must be added to the form before SetSystemImageListHandle is called.

Here is the version that uses the SystemImageList

Form-DisplayDirExpIcons.zip (1.09 kb)

It might be displayed like this

Form-DisplayDirExp3

A useful thing to set on the ListView control is AutoResizeColumns. This can have one of two values, HeaderSize or ColumnContent and resizes the columns depending on the width of the text.

$LISTVIEW.AutoResizeColumns([Windows.Forms.ColumnHeaderAutoResizeStyle]::HeaderSize)

Another is AllowColumnReorder. This allows you to move the columns around as you like

$LISTVIEW.AllowColumnReorder = $true

With all of the attributes that you can have in the xml it might be handy to specify which columns you want displayed in the ListView. This can be done by passing an array of attribute names that will be displayed as columns. Here Get-DirAsXml adds 9 attributes (Name + 2props + 6extendedProps)

PS> Form-DisplayDirExp (Get-DirAsXml test -props @{Length="";LastWriteTime=""} -ExtendedProps @{Title=""; Subject=""; Author=""; Category=""; Keywords=""; Comments=""}) -columns @("Name", "Length", "LastWriteTime")

So instead of displaying all 9 file properties it will only display Name, Length and LastWriteTime in that order. But supposing you DO want to see the other file properties? You could add a third pane to the Explorer


    $LISTVIEW2 = new-object windows.forms.ListView
    $FORM.Controls.Add($LISTVIEW2)
    $LISTVIEW2.Location = new-object System.Drawing.Size(600, 0)
    $LISTVIEW2.Size = new-object System.Drawing.Size(295,269)
    $LISTVIEW2.Anchor = "top, bottom, right"
    $LISTVIEW2.View = [System.Windows.Forms.View]::Details
    [void]$LISTVIEW2.Columns.Add("Name", -2, [windows.forms.HorizontalAlignment]::Left)
    [void]$LISTVIEW2.Columns.Add("Value", -2, [windows.forms.HorizontalAlignment]::Right)
    $LISTVIEW.add_ItemActivate({
        $LISTVIEW2.Items.Clear()
        $xmlnode = $LISTVIEW.SelectedItems[0].Tag
        foreach($att in $xmlnode.Attributes){
            $item = new-object windows.forms.ListViewItem($att.get_Name())
            $item.SubItems.Add($att.Value)
            $LISTVIEW2.Items.Add($item)
        }
    })
  

It might be displayed like this 

Form-DisplayDirExpIcon3Pane

Here is the code

Form-DisplayDirExpIcons3Pane.zip (1.27 kb)

You could set the ToolTipText on the ListViewItems. First set ShowItemToolTips to true. This must be done before you set the SystemImageList handle if you are using icons.

$LISTVIEW.ShowItemToolTips = $true

Then build up your own ToolTipText or if you specified -ExtendedProps @{InfoTip=""} on Get-DirAsXml then you can use it as you populate the ListView control

$item.ToolTipText = $child.InfoTip

This might be displayed like this (with cursor)

Form-DisplayDirExpToolTip

Adding a context menu to the ListView is quite easy.


    $LISTVIEW.ContextMenu = $cm = new-object Windows.Forms.ContextMenu
    $mi = $cm.MenuItems.Add("Do Thing One")
    $mi.add_Click({
        $XMLNODE = $LISTVIEWNODE.Tag
        Write-Host "Thing One invoked on $LISTVIEWNODE."
    })
    $mi = $cm.MenuItems.Add("Do Thing Two")
    $mi.add_Click({
        $XMLNODE = $LISTVIEWNODE.Tag
        Write-Host "Thing Two invoked on $LISTVIEWNODE."
    })
    $LISTVIEW.add_MouseDown({
        if ($_.Button -eq [Windows.Forms.MouseButtons]::Right){
            $LISTVIEWNODE = $LISTVIEW.GetItemAt($_.X, $_.Y)
        }else{
            $LISTVIEWNODE = $null
        }
    })

This might be displayed like this

Form-DisplayDirExpMenu

On the Host console you will see

Form-DisplayDirExpMenuO

Here is the code

Form-DisplayDirExpMenu.zip (1.29 kb)

With all those ExtendedProperties you can get from Get-DirAsXml a search functionality is mighty useful. Here is some code to add an XPath search to the explorer. Any valid XPath can be used such as //file[contains(@Author, 'Chr')]  or //file[contains(@BitRate, '44')]. A find duplicate file names: //file[@Name = preceding::file/@Name]. See this previous entry for how to add a Checksum attribute then find duplicate files //file[@Checksum = preceding::file/@Checksum].

   
    $SEARCHBOX = new-object windows.forms.TextBox
    $SEARCHBOX.Location = new-object System.Drawing.Point(85, 275)
    $SEARCHBOX.Size = new-object System.Drawing.Size(490,20)
    $SEARCHBOX.Anchor = "left, bottom, right"
    $FORM.Controls.Add($SEARCHBOX)
    $BUTTON = new-object windows.forms.button
    $BUTTON.Location = new-object System.Drawing.Point(10, 275)
    $BUTTON.Anchor = "left, bottom"
    $BUTTON.Text = "Search"
    $BUTTON.add_Click({
        $LISTVIEW.Items.Clear()
        $nodes = $XML.SelectNodes($SEARCHBOX.Text)
        foreach($child in $nodes){
            $idx = [cjb.Shell32]::GetSystemImageListIndex($child.Name, ($child.get_Name() -eq "folder"), $SHGFI_SMALLICON)
            $item = new-object windows.forms.ListViewItem($child.Name, $idx)
            $item.Tag = $child
            $item.ToolTipText = $child.InfoTip
            foreach($column in ($LISTVIEW.Columns|where{$_.Text -ne "Name"})){
                if ($child.($column.Text) -ne $null){
                    $item.SubItems.Add($child.($column.Text))
                }else{
                    $item.SubItems.Add("")
                }
            }
            $LISTVIEW.Items.Add($item)
        }
        $LISTVIEW.AutoResizeColumns([Windows.Forms.ColumnHeaderAutoResizeStyle]::ColumnContent)
    })
    $FORM.Controls.Add($BUTTON)
   

It might be displayed like this (with cursor)

Form-DisplayDirExpSearch

Here is the code

Form-DisplayDirExpSearch.zip (1.26 kb)

Another thing you might want to do is drag'n'drop. I will go back to the 3 pane solution but with a ListView in the middle and a TreeView on either side. The drag'n'drop rule is simple. Things can be dragged only from the ListView to the right side TreeView.

   
    $LISTVIEW.add_ItemDrag({
        param($sender, $e)
        $nodes = @()
        foreach($lvi in $sender.SelectedItems){
            $nodes += $lvi.Tag
        }
        $sender.DoDragDrop($nodes, [System.Windows.Forms.DragDropEffects]::Link)
    })
   
    $TREEVIEW2.add_DragDrop({
        param($sender, $e)
        $point = new-object System.Drawing.Point -ArgumentList @($e.X, $e.Y)
        $targetPoint = $sender.PointToClient($point)
        $dropnode = $sender.GetNodeAt($targetPoint)
        if ($dropnode.Tag.get_Name() -eq "folder"){
            $nodes = $e.Data.GetData("System.Object[]")
            foreach($n in $nodes){
                $newnode = $XML2.ImportNode($n, $true)
                $dropnode.Tag.AppendChild($newnode)
            }
            $dropnode.Nodes.Clear()
            DisplayDir_wander ($dropnode.Tag) ($dropnode) ($true)
        }
    })

It might be displayed like this (without cursor)

Form-DisplayDirExpDragDrop 

So imagine that the left is a list of all files in a tree, on the right are groups and topics in a knowledgebase. It might be a movie or mp3 library with moods or genres. It needs human interaction to decide how the resulting xml is layed out and what it contains. Here is a possibility, it takes the usual as input plus another xml that it creates on the fly. The output when the form is closed is sent to the T-Dax2Wpl translet to create a Windows Media Player playlist. Or it might be an inventory list for a software release and all the resulting files are copied to an output media.

PS> Form-DisplayDirExp (Get-DirAsXml test -props @{Length="";LastWriteTime=""} -ExtendedProps @{Title="";Subject=""; Author="";Category="";Keywords=""; Comments=""}) -columns @("Name", "Length", "LastWriteTime") -xml2 ([xml]'<root><folder Name="Knowledge"><folder Name="Topic" /><folder Name="Group" /></folder></root>') | T-Dax2Wpl

Here is the code

Form-DisplayDirExpDragDrop.zip (1.72 kb)

Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

Add comment