So I'm attempting to write a PowerShell script with a GUI input; idea being that the end users see a nested TreeView of the file system. They tick a some folders, hit a button, …magic happens.
I have the mount points (Not sure what's happening with C yet), problem is i can't seem to figure out / find how to enumerate through and add as nested nodes.
My search results all seems to point me to C#, C++, etc examples; not the helpful.
Ideas?
This is what i got
This is what i want (but keeps going deeper)
So this is my code so far
$objDriveLetters = GET-WMIOBJECT –query "SELECT * from win32_logicaldisk"
$form = New-Object System.Windows.Forms.Form
$treeView = New-Object System.Windows.Forms.TreeView
$treeView.Dock = 'Fill'
$treeView.CheckBoxes = $true
foreach ($iDrive in $objDriveLetters)
{
$DriveRoot = Get-Item $iDrive.DeviceID
#$FolderRoot = Get-ChildItem -Path $iDrive.DeviceID
$FolderRoot = Get-Item -Path $iDrive.DeviceID
$treeView.Nodes.Add($FolderRoot.FullName, $FolderRoot.FullName)
}
$form.Controls.Add($treeView)
$form.ShowDialog()
Best Answer
It's simple as
treeView.Nodes.Add
method returns the TreeNode that was added to the collection i.e. an object of[System.Windows.Forms.TreeNode]
type. Therefore, you can applyAdd
method on it to create new tree nodes corresponding to filesystem nested items as follows:Result:
Explanation (note comment lines inside code as well): chiefly notice that:
where Drivetype=4
clause merely for my debugging purposes, andAddNodes
subroutine calls itself recursively for subfolders if any.