Here is a script I wrote a couple of years ago that I find useful.  It
is a batch file renaming perl script, so you can, for example, do
regex search and replace on file names, like, converting all *.jpeg to
*.jpg with:

  ecp -r -f \.jpeg -t .jpg *.jpeg

It has a couple of nice features:

1) It has a  -l option to list what changes would be made, without
   actually making them.  Very helpful for tweaking regexps

2) It has case conversion options

3) It can regularize digits lengths, so if you have
   file1,file2,...,file999, you can convert these to
   file001,file002,...,file999 so the lexical ordering is correct

4) Prepend prefixes or append suffixes to all files

5) Complete perldoc, and doesn't require any nonstandard modules



As I said, I wrote this several years ago when I was wetter behind the
ears, so I am sure there is much that could be improved

Attachment: ecp
Description: Binary data


Here's the perldoc with lots-o-examples; enjoy:

NAME
    ecp - enhanced cp w/ features like REGEXP replace on file names

SYNOPSIS
    ecp [OPTIONS]... TARGETS

DESCRIPTION
    Rename or copy files specified by TARGETS, changing the file names with
    a bunch of options. Main features include REGEXP replace, case
    conversion, integer length conversion (e.g. replace '1' with '01' or
    '001' etc...). Rename, copy or simply list OLDNAME, NEWNAME to let you
    tweak the REGEXPs. Force unique file names to avoid clobbering existing
    files. And much, much more! By default all files are copied and new
    files are placed in current directory.

REQUIREMENTS
    Perl5. You may need to change the first line of this script to point to
    your perl executable.

OPTIONS
    -c  Convert all file names to lower case. I often transfer DOS files to
        a UNIX environment via ftp and the buggars come through uppercase.

    -d DESTINATION
        Copy or move all TARGETS to directory DESTINATION. Default is
        current directory.

    -f REGEXP
        Replace the regular expression REGEXP with "-t" REPLACE-STRING (or
        strip matching strings from file name if "-t" isn't given). For your
        convenience, the program will output the "-f" REGEXP and "-t"
        REPLACE-STRING to help you see what the program is getting so that
        you may deal with shell quoting conventions.

    -i  Do case insensitive REGEXP replace.

    -l  List the old file names and new file names but do not copy or rename
        (practice w/ REGEXP makes perfect).

    -n N
        Replace all integers with an N length version, e.g.

        "-n"2 1to1.txt-> 01to01.txt

        "-n"3 1234to1.txt -> 1234to001.txt

        In many cases the default UNIX file order ('john10' before 'john2')
        is not what I intended hwen i named the files. The flag "-n"2
        replaces 'john2' w/ 'john02' and then UNIX puts the files in the
        order I intended.

    -p PREFIX
        Prepend PREFIX to each TARGET

    -s SUFFIX
        Append SUFFIX to each TARGET

    -t REPLACE-STRING
        Replace all matches of the "-f" REGEXP with REPLACE-STRING

    -u  Force unique file names (don't overwrite existing files) by
        appending the extension '.1' if a file with the computed file name
        exists in the destination directory. If the computed file name ends
        in an integer and a file exists in the target directory with the
        computed name, the integer extension is incremented until the file
        name is unique.

EXAMPLES
    Add prefix 'mary' to all files with '.dat' extension.

    > ecp -rp mary *.dat

    Add suffix '.dat' to all files beginning with 'mary'.

    > ecp -rs .dat mary*

    Replace 'mary' with 'data' in all file names

    > ecp -rf mary -t data *

    Hide all files with extension '.cr'

    > ecp -rp . *.cr

    List oldname/newname pairs, replacing all '.dat' extensions with
    '.dat.gz' and padding all integers in the file name with zeros so that
    they are at least three characters long ('mary1.dat' ->
    'mary001.dat.gz'). The forward slashes '\' and single quotes in the
    examples below are for csh quoting.

    > ecp -ln3 -f '\.dat' -t .dat.gz *.dat

    Replace all file name extensions 'jpg' or 'gif' with extension 'jdh'.
    This example, because of the anchor '$', will convert 'test.jpg' ->
    'test.jdh' but leave 'test.jpg.gz' untouched.

    > ecp -rf '.(jpg|gif)$' -t .jdh *

    Careful with those REGEXPs! The following two examples show what can go
    wrong if a stray '\' is omitted. The list output from the -l flag is
    included. The goal is to replace all file names with extensions '.jpg'
    or '.gif' with the extension '.jdh'.

    Wrong!

    > ecp -lf '.(gif|jpg)$' -t .jdh test*

    from string: .(gif|jpg)$

    to string: .jdh

    test.gif to ./test.jdh

    test.jpg to ./test.jdh

    test.jpg.gz to ./test.jpg.gz

    testwjpg to ./test.jdh

    testwjpg.gz to ./testwjpg.gz

    Correct!

    > ecp -lf '\.(gif|jpg)$' -ty .jdh test*

    from string: \.(gif|jpg)$

    to string: .jdh

    test.gif to ./test.jdh

    test.jpg to ./test.jdh

    test.jpg.gz to ./test.jpg.gz

    testwjpg to ./testwjpg

    testwjpg.gz to ./testwjpg.gz

AUTHOR
    John Hunter [EMAIL PROTECTED] 5/28/99









Reply via email to