Folder content

En partant d’un dossier racine donné, cette méthode renseigne un objet décrivant le contenu du dossier.

<code 4D>
//FS_folderContent (obj)
//on first call, pass $1 root in POSIX syntax ending with “/”
//$1 obj:
// path: mandatory, POSIX folder path ending with “/”
// depth: optional, set to 1 if not set by caller
// depthMax: optional, set to MAXINT (=no limit) if not set by caller
//# recursive
C_OBJECT($1;$io)
C_TEXT($systemPath_t)
C_LONGINT($i_l)
ARRAY TEXT($document_at;0)
ARRAY TEXT($folder_at;0)
//_
$io:=$1
ASSERT($io.path#Null)
$i_l:=Length($io.path)
ASSERT($io.path[$i_l]="/")
//ASSERT($io.path[Length($io.path]="/") //syntaxe non valide ???
$systemPath_t:=Convert path POSIX to system($io.path)
If ($io.depth=Null)
$io.depth:=1
End if
If ($io.depthMax=Null)
$io.depthMax:=MAXINT //no limit (but dangerous…)
End if

DOCUMENT LIST($systemPath_t;$document_at;Ignore invisible+Absolute path+Posix path)
If (Size of array($document_at)>0)
$io.documents:=New collection
ARRAY TO COLLECTION($io.documents;$document_at)
End if

FOLDER LIST($systemPath_t;$folder_at)
If (Size of array($folder_at)>0)
C_COLLECTION($folder_c)
C_OBJECT($folder_o)
$folder_c:=New collection()
ARRAY TO COLLECTION($folder_c;$folder_at)
$io.folders:=New collection
For each ($folder_t;$folder_c)
$folder_o:=New object
$folder_o.path:=$io.path+$folder_t+"/"
$folder_o.depth:=$io.depth+1
$folder_o.depthMax:=$io.depthMax
If ($folder_o.depth<$io.depthMax)
FS_folderContent ($folder_o)
End if
OB REMOVE($folder_o;“depth”)
OB REMOVE($folder_o;“depthMax”)
$io.folders.push($folder_o)
End for each
OB REMOVE($io;“depth”)
OB REMOVE($io;“depthMax”)
End if
//_
</code 4D>

Exemple, lire le contenu du dossier “Bureau” à la “profondeur” maximum 3 :
<code 4D>
$io:=New object
$io.path:=Convert path system to POSIX(System folder(Desktop))
$io.depthMax:=3
FS_folderContent ($io)
SET TEXT TO PASTEBOARD(JSON Stringify($io;*))
</code 4D>

Et en récursif, bravo, c’est super.