Subject: bug#6823: 24.0.50; Wdired or Dired enhancement

From: Thierry Volpiatto <[email-masked]>
Date: Mon, 09 Aug 2010 07:50:31 +0200

Hi Drew,

"Drew Adams" <[email-masked]> writes:

> Emacs sorely needs one or more commands that let you rename the
> marked files as a sequence (following a pattern).
>  
> See MS Windows for an example (but we could do better):
>  
> 1. You sort the file list the way you want, to put the files you want to
> act on in the right order.  Especially with libraries such as Francis
> Wright's `dired-sort-menu.el', you can sort Dired in many different
> ways.
>  
> 2. You mark the files you want to rename.  (In Windows you select them.)
>  
> 3. You invoke a rename command, and enter a pattern for the new names.
> The pattern includes an optional starting index, which is a whole
> number.  In Windows you put the index in parens, which are included in
> the file names.  Example: `new name (100)'.  The marked files are named
> `new name (100)', `new name (101)'...
>  
> Obviously, in Emacs we could provide for better patterns and
> substitutions than this.  But AFAIK today we offer nothing like this.
>
> If you have 1000 family photo files you want to rename to something like
> `2010 Summer Vacation (1000)', `2010 Summer Vacation (1001)' etc. (or
> even just `1000', `1001'...), then AFAIK the best you can do now is to
> use Wdired and perform query-replace with some fancy replacement
> expression.  We should offer something simpler for the common task of
> renaming a sequence of files.

We have have two tools to achieve this, wdired-change-to-wdired-mode,
and query-replace-regexp used with \, and \# in the replacement regexp.

,----
| E.g: (in dired)
| C-x C-q
| C-M-%
| ==> [A-Za-z0-9]*.jpg
| ==> 2010-summer-vaccation-\,(format "1%03d" \#).jpg
`----

You can use also this function:(You may have to run it two times in some
rare cases)

,----
| (defun serial-rename (dir ext name start)
|   "rename all the files of DIR matching regex EXT with the name NAME \
| starting to number START - ex: file01.jpg"
|   (interactive "Ddir: \nsExt(no dot): \nsName: \nnStart: ")
|   (find-file dir)
|   (let* ((ls-dir     (file-expand-wildcards (format "*.%s" ext) t))
|          (new-ls-dir (loop with len = (length ls-dir) 
|                         repeat len for count from start
|                         for fnum = (if (< start 10) "0%s" "%s")
|                         collect (concat dir name (format fnum count) "." ext))))
|     (loop for i in ls-dir for index from 0
|        for new-name = (nth index new-ls-dir)
|        unless (file-exists-p new-name) do (rename-file i new-name)))
|   (revert-buffer nil t nil))
`----

-- 
Thierry Volpiatto
Get my Gnupg key:
gpg --keyserver pgp.mit.edu --recv-keys 59F29997