Build directory structure before writing file