0 - 1 of 1 tags for db-hierarchy

I'm struggling with a query to fetch entire database hierarchy and calculating path of each node from root. I need to create same directory structure on the file system whereby each directory represents a database.
 
Example
 
RootDB
--Level1-Child1
----Level2-Child1
----Level2-Child2