1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

renx.btm, extended rename

Discussion in 'T&T - Scripting' started by epement, Jan 6, 2012.

  1. epement

    Jun 28, 2008
    Likes Received:
    Some of the users here may find this batch file helpful:

    @echo off
    :: filename: renx.btm
    ::   author: Eric Pement
    ::     Date: Dec. 19, 2003
    ::   Credit: Tom Christiansen and Nathan Torkington, "Perl Cookbook" (1998), p. 327
    switch "%1"
    case "" .OR. "-?" .OR. "-h" .OR. "--help"
      goto syntax
      if not exist "c:\bat\lib\renx.pl" (echo File c:\bat\lib\renx.pl not found! %+quit)
      :: Let perl process all the filename globbing and all the switches.
      perl c:\bat\lib\renx.pl %$
    goto end
    renX - extended rename, a Perl file renamer
           Show "renaming OldName -> NewName" and total # of files renamed
       renx [-options] "script" [files morefiles* ...]
       -n    Do Nothing. Show what the results would be if files were renamed
       -q    Quiet mode. Do not display any results to the console
       -v    Verbose. Display renaming results and command-line arguments
      -h,-?  Display this help message
       renx -q "s/\.orig$//" *.orig         # cut trailing extension quietly
       renx "tr/A-Z/a-z/ unless /^Make/" *  # convert to lowercase except ...
       renx "$_ .= '.bad'" *.f              # append .bad to each file named
       renx "s/^(.+)~$/.#$1/" *             # del trailing ~ and prepend '.#'
    The file "renx.pl" that it calls is below . . .

    # filename: renx.pl - Larry Wall's filename fixer
    #   author: Eric Pement
    #     date: 2004-09-24
    # modified: 2005-08-15
    # Originally written by Larry Wall. Basic script in Tom Christiansen and
    # Nathan Torkington, "Perl Cookbook" (1998), p. 327, to rename files.
    # I expanded it to work on a DOS/Windows filesystem, and to support quiet
    # and verbose option switches, and totals of files changed. -- ENP.
    use strict;
    use warnings;
    use diagnostics;
    use Getopt::Std;
    use File::DosGlob 'glob';
    # Foreach character X in the set (nqv), assign $opt_X to 1.
    # -n       = Do nothing. Don't rename, but show results of proposed action.
    # -q       = Quiet mode. Do not display any results to the console.
    # -v       = Verbose. Display renaming prompt and command-line arguments.
    # default  = display "renaming OldName -> NewName" and total files renamed.
    my $op = shift or die `renx --help`;   # print help msg from 4DOS parent file
    print "Verbose output:\n"                 if $opt_v;
    print "  \$op is        [$op]\n"          if $opt_v;
    # create @ARGV from stdin, if necessary
    chomp(@ARGV = <STDIN>) unless @ARGV;
    # expand the command tail, which may look like "s*.*,*.htm,*.h"
    my @filelist  = glob("@ARGV");
    # remove duplicate files from filelist
    my %uniquefiles = ();
    foreach my $file (@filelist){
    # revise @filelist to contain only unique, valid filenames
    my @uniqfilz = sort keys %uniquefiles;
    print "  \@ARGV is      [@ARGV]\n"        if $opt_v;
    print "  \@filelist is  [@filelist]\n"    if $opt_v;
    print "  \@uniqfilz is  [@uniqfilz]\n\n"  if $opt_v;
    # get the longest filename:
    my $longest = 0;
    for (@uniqfilz) {
       $longest = length($_) if length($_) > $longest;
    # Finally! rename the files, printing the changes unless quelled by -q
    # or unless modified by -n
    my $sum = 0;
    for (@uniqfilz) {
       my $was = $_;
       eval $op;
       die $@ if $@;
       unless ($was eq $_) {
          printf("renaming %-*s  ->  %s\n", $longest, $was, $_) unless $opt_q;
          rename($was,$_) unless $opt_n;
    print "Total of $sum file" . ($sum==1 ? "" : "s")
          . ($opt_n ? " would be" : "" ) . " renamed.\n"   unless $opt_q;
    print "No files were renamed.\n"                      if $opt_n;
    #---end of script---
    Hope someone finds this useful !


Share This Page