title: How to get list of all leaf folders from ADLS Gen2 path via Scala code?
body: <p>We have folders and subfolders in it with year,month, day folders in it. How can we get only the last leaf level folder list using dbutils.fs.ls utility?</p>
  <p>Example path:</p>
  <p>abfss://cont@storage.dfs.core.windows.net/customer/data/V1/2021/
  abfss://cont@storage.dfs.core.windows.net/customer/data/V1/2022/
  abfss://cont@storage.dfs.core.windows.net/customer/data/V1/2022/03/24/15/a.parquet
  abfss://cont@storage.dfs.core.windows.net/customer/data/V1/2022/03/25/15/b.parquet
  .
  .</p>
  <p>The function should return only last leaf level folder list i.e</p>
  <p>abfss://cont@storage.dfs.core.windows.net/customer/data/V1/2022/03/24/15
  abfss://cont@storage.dfs.core.windows.net/customer/data/V1/2022/03/25/15</p>
  <p>EDIT:</p>
  <p>I have tried below function and it works but it fails when some folder is empty with error &quot;java.lang.UnsupportedOperationException: empty.reduceLeft&quot;. Please help.</p>
  <pre><code>def listLeafDirectories(path: String): Array[String] =
    dbutils.fs.ls(path).map(file =&gt; {
      // Work around double encoding bug
      val path = file.path.replace(&quot;%25&quot;, &quot;%&quot;).replace(&quot;%25&quot;, &quot;%&quot;)
      if (file.isDir) listLeafDirectories(path)
      else Array[String](path.substring(0,path.lastIndexOf(&quot;/&quot;)+1))
    }).reduce(_ ++ _).distinct
  </code></pre>