help-gnu-emacs
[Top][All Lists]
Advanced

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

Re: Easy way to rename files sequentially?


From: Thierry Volpiatto
Subject: Re: Easy way to rename files sequentially?
Date: Tue, 27 Nov 2007 16:14:47 +0100
User-agent: Gnus/5.11 (Gnus v5.11) Emacs/23.0.60 (gnu/linux)

Sebastian Tennant <address@hidden> writes:

> Quoth Thierry Volpiatto <address@hidden>:
>> I would like to do some thing like that:
>>
>>   (shell-command "~/bin/serialrename.py -d dir -e txt"))
>
> I believe what you want can be achieved like this:
>
>  (shell-command (format "~/bin/serialrename.py -d %s -e txt" dir))
>
Thank you again, i did that, it work fine :)

(setq serial-rename-command "~/bin/serialrename.py")

(defun tv-serial-rename (dir ext name start)
  (interactive "sDir: \nsExt: \nsName: \nsStart: ")
  (find-file dir)
  (shell-command 
   (format "%s -d %s -e %s -n %s -s %s" 
           serial-rename-command dir ext name start)))

Ci joint le script python:
#!/usr/bin/python
# -*- coding: utf-8 -*-
#       $Id: serialrename.py,v 1.5 2007/11/27 15:05:06 thierry Exp $    


#commentary:
#Rename the content of a directory
#+with incremental numbers
#+ for a specific extension file

import os
from optparse import OptionParser
import sys

parser = OptionParser()

parser.add_option("-d",
                  "--dir",
                  dest="rep",
                  help ="Input directory(with / at the end please)")
parser.add_option("-n",
                  "--name",
                  dest="nom",
                  default="file",
                  help ="optional:file name")
parser.add_option("-s",
                  "--start-number",
                  dest="startn",
                  default=101,
                  help ="optional:initial incremental number")
parser.add_option("-e",
                  "--ext",
                  dest="extension",
                  help ="file type - ex: jpg(without dot)")

(options, args) = parser.parse_args()

rep = options.rep
nom = options.nom
startn = int(options.startn)
extension = options.extension

if len(sys.argv) < 3:
    parser.error("Serialrename take at list 2 arguments\n you must specify 
options -d and -e")
else:
    try:
        dir_photos = os.listdir(rep)
        for i in dir_photos:
            if extension in i:
                new = rep + nom + str(startn) + "." + extension
                if new not in dir_photos:
                    print new
                    os.rename(rep + i, new)
                    startn += 1
                else:
                    print new, "new"
                    os.rename(rep + i, new + "new")
                    startn += 1
    except (IOError, TypeError):
        raise "Check the path of your dir - Have you forget the / at the end ?"
-- 
A + Thierry

reply via email to

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