0

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
    }
  }
}
  • 2
    I am not quiet sure where you're heading to but in any case, [`$Null` should be on the left-hand side of the equality operator](https://stackoverflow.com/a/49411823/1701026): `if($Null -ne $foldersToCreate.foldersToCreate) {...`. Especially for when it concerns a collection. – iRon Apr 20 '20 at 14:37
  • 1
    I don't understand your problem : why test2 not supposed to be created in path1 ? It corresponds to the structure of the json given in example. – PlageMan Apr 20 '20 at 14:43
  • What is $blockedcomments and $cleantext? – js2010 Apr 20 '20 at 15:06

0 Answers0