Subject: (Feature request w/ patch) wdired should create directories needed for destination files

From: Phil Sung <[email-masked]>
Date: Fri, 6 Aug 2010 14:26:04 -0700

It is sometimes useful in wdired to be able to move files into
directories that don't yet exist, e.g. when one is reorganizing files
and directories. The attached patch (against git HEAD), based on
previous work by Joakim Verona (source:
http://www.emacswiki.org/emacs/WDired), makes wdired create parent
directories needed as necessary for the destination files, conditional
on the variable `wdired-create-parent-directories'.

What do others think of this feature?

Regards,
Phil
diff --git a/lisp/wdired.el b/lisp/wdired.el
index 375bc26..c8997f3 100644
--- a/lisp/wdired.el
+++ b/lisp/wdired.el
@@ -168,6 +168,16 @@ program `dired-chmod-program', which must exist."
 		 (other :tag "Bits freely editable" advanced))
   :group 'wdired)
 
+(defcustom wdired-create-parent-directories nil
+  "If non-nil, create parent directories of destination files.
+
+If non-nil, when you rename a file to a destination path within a
+nonexistent directory, wdired will create any parent directories
+necessary. When nil, attempts to rename a file into a nonexistent
+directory will fail."
+  :type 'boolean
+  :group 'wdired)
+
 (defvar wdired-mode-map
   (let ((map (make-sparse-keymap)))
     (define-key map "\C-x\C-s" 'wdired-finish-edit)
@@ -478,6 +488,8 @@ non-nil means return old filename."
               (require 'dired-aux)
               (condition-case err
                   (let ((dired-backup-overwrite nil))
+                    (and wdired-create-parent-directories
+                         (wdired-create-parentdirs file-new))
                     (dired-rename-file file-ori file-new
                                        overwrite))
                 (error
@@ -487,6 +499,11 @@ non-nil means return old filename."
                             err)))))))))
     errors))
 
+(defun wdired-create-parentdirs (file-new)
+  "Create parent directories for FILE-NEW if they don't exist."
+  (and (not (file-exists-p (file-name-directory file-new)))
+       (message "Creating directory for file %s" file-new)
+       (make-directory (file-name-directory file-new) t)))
 
 (defun wdired-exit ()
   "Exit wdired and return to dired mode.