Does anyone have an emacs lisp hack that would allow the creation of a new directory on the fly during dired-do-copy or dired-do-rename? I understand that it can be created prior to running one of these two commands. Extra points for some type of "Are you sure..." prompt.
Asked
Active
Viewed 475 times
1 Answers
5
It look like a case of applying an advice. The question being: what to
advice. Looking at the dired code, it seem that the correct target is
dired-mark-read-file-name
that is used to read the destination
file-name. This will work:
(defadvice dired-mark-read-file-name (after rv:dired-create-dir-when-needed (prompt dir op-symbol arg files &optional default) activate)
(when (member op-symbol '(copy move))
(let ((directory-name (if (< 1 (length files))
ad-return-value
(file-name-directory ad-return-value))))
(when (and (not (file-directory-p directory-name))
(y-or-n-p (format "directory %s doesn't exist, create it?" directory-name)))
(make-directory directory-name t)))))
Note that maybe the first when
(when (member op-symbol '(copy move))
) could be removed for this to apply to more case of file creation in dired. But I'm not sure of when dired-mark-read-file-name is called, So I let this test there to reduce potential unwanted side-effect

Rémi
- 8,172
- 2
- 27
- 32
-
Tried a test by evaluating the advice defun and using dired-do-copy "C" It errored out with the following message. Copy `c:/Users/family/AppData/Roaming/temp.emacs' to `c:/Users/family/AppData/Roaming/temp110/temp.txt' failed: (file-error Copying file no such file or directory c:/Users/family/AppData/Roaming/temp.emacs c:/Users/family/AppData/Roaming/temp110/temp.txt) I am using emacs 24.2 under windows and dired+ which may redefine dired-do-copy. Any suggestions? – Mikef Oct 21 '12 at 23:46
-
It doesn't take care of this case. What my code is doing is creating the directories if either there are several file to copy or move, or there if you have given a directory name as answer to where to copy... I will look at a better solution If I've time... – Rémi Oct 22 '12 at 07:35
-
Okay, I fixed it, It's even simpler like that. – Rémi Oct 22 '12 at 07:47
-
Works well, tried several cases. EMACS should work this way by default. Thanks Mike – Mikef Oct 22 '12 at 13:09