1 min read
Convert Directory Tree to Json with PowerShell
Today I wrote a simple script that converts a directory tree query with get-childitem
into a json formatted data tree.
function Add-Tabstops{
param($Count)
$tabs = ""
for($i=0; $i -lt $Count; $i++){$tabs += " "}
return $tabs
}
function Output-JsonChildren{
param($Path, $Level = 1)
return $(Get-ChildItem -Path $Path | Where-Object{$_} | ForEach-Object{
(Add-Tabstops $Level) +
"{`n" +
(Add-Tabstops ($Level+1)) +
"`"name`"`: `"$($_.Name)`"," +
"`n" +
(Add-Tabstops ($Level+1)) +
"`"children`": ["+
$(if($_.psiscontainer){"`n" + (Output-JsonChildren -Path $_.FullName -Level ($Level+2))+ "`n" + (Add-Tabstops ($Level+1))}) +
"]`n" +
(Add-Tabstops ($Level)) +
"}"
}) -join ",`n"
}
$JSON = Output-JsonChildren -Path "C:\Documents"
"["
$JSON
"]"
Find the latest version of this script here: https://gist.github.com/7c9e9ea0acd77c965b79
The output looks something like this:
[
{
"name": "Administration",
"children": [
{
"name": "Organisation",
"children": [
{
"name": "Available Abrevations.md",
"children": []
},
{
"name": "Kürzel Gemeinde und Organisationen.md",
"children": []
}
]
},
Categories:
Scripting
Tags: powershell
Edit this page
Show statistic for this page