create missing directories when writing in file + misc