Filesystem cypher query

Hey,

I have I think a simple query to build but I already spend on it 3 hours without success.

I have in the db structure that represents filesystem:
We have multiple directories, each directory contains other directories, files or nothing.

I would like to query whole tree.

The following query does only part of the job. It returns the structure, but does not return empty directories

match (a:Root)-[k:CHILD*]->(r:Directory)-[h:CHILD*]->(l:File) return a,k,r,h,l

On the other hand this returns the directory but not files.

match (a:Root)-[k:CHILD*]->(r:Directory) return a,k,r

Do you have any suggestion how to write this query?

You could try this. Sorry, I don't have any data to test it on. It should find each root node and return a list of directories under the root. Each directory will have the nodes along the path and a list of the files within the directory. Let me know if it does not work or needs some tweaking.

match p=(a:Root)-[k:CHILD*]->(r:Directory)
with a, [i in nodes(p) where "CHILD" in labels(i)] as path, r
return a, collect({
    directory: r,
    path: path,
    files: [(r)-[:CHILD]->(l:File) | l]
}) as directories

The query I provided will not show the directories within the directory. You can try this if that is important.

match p=(a:Root)-[k:CHILD*]->(r:Directory)
with a, [i in nodes(p) where "CHILD" in labels(i)] as path, r
return a, collect({
    directory: r,
    path: path,
    subdirectories: [(r)-[:CHILD]->(l:Directory) | l],
    files: [(r)-[:CHILD]->(l:File) | l]
}) as directories