[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to rename files to numbers in Eshell?

From: Jean Louis
Subject: Re: How to rename files to numbers in Eshell?
Date: Sun, 12 Sep 2021 22:03:54 +0300
User-agent: Mutt/2.0.7+183 (3d24855) (2021-05-28)

* Felix E. Klee <> [2021-09-12 11:46]:
> Jean Louis <> writes:
> > (defun renumber-files (&optional files)
> > […]
> Thanks for the alternative approach, Jean!
> It’s still quite verbose, though, even involving the creation of a
> custom function.
> Renaming files to numbers is actually very easy using Dired’s
> `wdired-change-to-wdired-mode`, and then doing regexp replace.  The
> purpose of my question is to learn a bit about Eshell.

Is there any regular expression counter replacement so that each new
replacement get a new number?

> > I have chosen to renumber it by date, you can customize it
> > yourself. It is handy when I am downloading many pictures or videos,
> > then I know at what date they were downloaded. Of course in that case
> > I don't mind of file names significance, that is why I am renumbering
> > them.
> For organizing imported media files by date, I have a Bash script
> `organize_media`:

I don't use Bash any more for that, I use Emacs Lisp.

For pictures, I made "sort-images", it is definitely not perfect for
everybody, but it serves me to sort those files that come from camera
devices or phone to sort them by year, month, date into their
corresponding folders.

(defun rcd-image-date-match (file)

(defun sort-images (&optional prefix)
  (interactive "P")
  (let ((files (dired-get-marked-files)))
    (dolist (file files)
      (let* ((attributes (file-attributes file))
             (file-base (file-name-base file))
             (file-ext (file-name-extension file))
             (position (rcd-image-date-match file-base))
             (modification-time (file-attribute-modification-time attributes))
             (year (if current-prefix-arg
                       (format-time-string "%Y" modification-time)
                       (substring file-base position (+ position 4))))
             (month (if current-prefix-arg
                        (format-time-string "%m" modification-time)
                      (substring file-base (+ 4 position) (+ position 6))))
             (day (if current-prefix-arg
                      (format-time-string "%d" modification-time)
                    (substring file-base (+ 6 position) (+ position 8))))
             (dir (concat "/home/data1/protected/Media/Pictures/Pictures/Year-" 
year "/" month "/" year "-" month "-" day "/"))
             (destination (concat 
"/home/data1/protected/Media/Pictures/Pictures/Year-" year "/" month "/" year 
"-" month "-" day "/" file-base "." file-ext)))
        (mkdir dir t)
        (message (concat "Copy file to: " destination))
        (if (file-exists-p destination)
            (if (y-or-n-p (concat "Overwrite: " destination))
                (rename-file file destination t))
          (rename-file file destination))))


Take action in Free Software Foundation campaigns:

In support of Richard M. Stallman

reply via email to

[Prev in Thread] Current Thread [Next in Thread]