I am trying to create nested folders in datalake using JSON schema in Powershell.
Although its works, it creates all nested folders for all the paths. I have added test json.
my Json=
{
"system": [
{
"filesystem": "ing",
"foldersToCreate": [
{
"path": "path1/",
"foldersToCreate": [
{
"path": "test1/"
},
{
"path": "test2/"
}
]
},
{
"path": "path2/",
"foldersToCreate": [
{
"path": "test2/"
}
]
}
]
}
]
}
Ex.
path1
-test1
-test2
Path2
-test2
The code is running but also creates test2 in path1. i suppose their is a problem with properties.
function Read-JsonConfig([string]$path)
{
$file = Get-Item $path
$plaintext = $file | Get-Content
$blockComments = '/\*(.*?)\*/'
$psObject = $cleantext | ConvertFrom-Json
return $psObject
}
function create-gen2-folders($folderConfig, $parentPath = '/')
{
$foldersToCreate = $folderConfig.foldersToCreate | Sort-Object { $_.path }
foreach($folderToCreate in $foldersToCreate)
{
$path = build-path $parentPath $folderToCreate.path
if($foldersToCreate.foldersToCreate -ne $null)
{
create-gen2-folders -folderConfig $folderToCreate -parentPath $path
}
}
}