Creating a new file in Emacs is as easy as opening it. If you ask Emacs to open a file which doesn’t exist, it won’t complain; It will open up a blank buffer pointing at it. It will even let you open a file in a directory that doesn’t exist (yet).
If you do this, Emacs will let you know, by displaying this message:
Use M-x make-directory RET RET to create the directory and its parents
Which is handy… but wouldn’t it be nice if we didn’t even have to do that? This being Emacs, we don’t.
(add-hook 'before-save-hook
'(lambda ()
(or (file-exists-p (file-name-directory buffer-file-name))
(make-directory (file-name-directory buffer-file-name) t))))
If you add that to your .emacs, you won’t have to. When you save your buffer, Emacs will check to see if the path exists. If it doesn’t, it will create it for you, including any parent directories.
Discussion