Professional Web Applications Themes

file renaming util in perl - PERL Beginners

Before starting to write my script designed to make filename changing easier I'd like to ask how to create an editable command line inside a perl script? ksh93 and earlier versions does this on user input by default. At least a primitive amount of editing is possible. The kind of situation this script is designed for is like this: We have a number of files already named with names that don't lend themselves to mass renaming. That is, no recurring stings or the like. Except maybe the extension and then maybe not all of them. In this situation its pretty ...

  1. #1

    Default file renaming util in perl

    Before starting to write my script designed to make filename changing
    easier I'd like to ask how to create an editable command line inside a
    perl script?

    ksh93 and earlier versions does this on user input by default. At
    least a primitive amount of editing is possible.

    The kind of situation this script is designed for is like this:

    We have a number of files already named with names that don't lend
    themselves to mass renaming. That is, no recurring stings or the
    like. Except maybe the extension and then maybe not all of them.

    In this situation its pretty much necessay to process each file
    individually by editing parts of its name or replaceing parts etc.

    I'd like to have perl generate a globbed list for me, then present
    each list memember on an editable command line where I'm allowed
    either vi or emacs like line editing to process the given file.

    When I hit enter, the current one is renamed seamlessly and the next
    one presented.

    My skill level isn't very high but I think I can handle the pre
    processing and post processing ok. What I need help with is creating
    a line editor inside my script.

    Can any one offer an outline scheme for something like that?

    reader@newsguy.com Guest

  2. #2

    Default Re: file renaming util in perl

    com (Peter Scott) writes:
     
    > [snip]
    >
    > Term::ReadLine[/ref]

    Ah yes. Any idea how to make perl insert a string (actually a file
    name from an earlier process into that command line?

    Just inserting a sting to test like this doesn't work. The sting
    isn't editable.

    From the perldoc example for Term::Readline

    use Term::ReadLine;
    my $term = new Term::ReadLine 'Simple Perl calc';
    my $prompt = "Enter your arithmetic expression: <SOMETHING HERE>";
    my $OUT = $term->OUT || \*STDOUT;
    while ( defined ($_ = $term->readline($prompt)) ) {
    my $res = eval($_);
    warn $ if $;
    print $OUT $res, "\n" unless $;
    $term->addhistory($_) if /\S/;
    }

    How can I get an editable file name in there programatically?

    reader@newsguy.com Guest

  3. #3

    Default Re: file renaming util in perl

    com (Peter Scott) writes:
     
    >
    > You need a more capable ReadLine package. Install
    > Term::ReadLine::Gnu and do:
    >
    > BEGIN { $ENV{PERL_RL} = 'Gnu' }
    > use Term::ReadLine;
    > my $term = new Term::ReadLine 'Simple Perl calc';
    > my $prompt = "Enter your arithmetic expression: ";
    > my $OUT = $term->OUT || \*STDOUT;
    > while ( defined ($_ = $term->readline($prompt, '<SOMETHING here>')) ) {
    > my $res = eval($_);
    > warn $ if $;
    > print $OUT $res, "\n" unless $;
    > $term->addhistory($_) if /\S/;
    > }[/ref]

    Haa... nifty. Thanks, that was the missing ingrediant.

    I think I'll try your patience once more and see if you'd care to
    comment or provide a sanity check on the script resulting from your
    input:
    (Its overcommented but that might mean I'll see what its supposed to
    do 6 mnths from now)


    #!/usr/local/bin/perl -w

    # Keywords: rena.pl - designed to hasten file renaming [ File names
    # presented one at a time on an editable cmdline]
    # Apr 30 23:32:40 2004 5
    # &&

    my $myscript;
    ($myscript = $0) =~ s:^.*/::;

    ## Make sure PERL_RL is not set to 'perl'
    BEGIN { $ENV{PERL_RL} = 'Gnu' }

    use Term::ReadLine;

    if($ARGV[0] =~ /help/i){
    usage();
    exit;
    }
    ## Catchall reg
    my $filereg = '[\d\w]';
    ## Default working dir
    my $wrkdir = "./";

    ## ========== BEGIN Getopts section ==========
    ## Declare vars inside qw()
    use vars qw($opt_d $opt_r );
    use Getopt::Std;
    my $optstr ="r:d:";
    getopts($optstr);

    if($opt_d){
    ## If $opt_d doesn't exist, show usage and die, else set $wrkdir
    if(! -d $opt_d){
    usage();
    die "Problem with <$opt_d>: $!";
    }else{
    $wrkdir = $opt_d;
    }
    }

    if($opt_r){
    ## Change the default regex
    $filereg = $opt_r;
    }
    opendir(WRKDIR,"$wrkdir")or die "Can't open $wrkdir: $!";
    chdir $wrkdir or die "Can't chdir to $wrkdir: $!";

    ## compile file regex
    $freg = qr/$filereg/;
    FilesToRename = grep { /$freg/ && -f "$_" }readdir(WRKDIR);
    close(WRKDIR);

    my $term = new Term::ReadLine 'Perl file rename utility';
    my $prompt = "(type \`p <RET>' to bypass current file, press \`Ctrl-c' to abort program)
    Filename to edit => ";
    ## tell perl where to send the output
    my $OUT = $term->OUT || \*STDOUT;

    for(FilesToRename){
    my ($reg, $cmpreg, $fname, $newname);
    $fname = $_;
    ## If we have input
    if ( defined ($_ = $term->readline($prompt, "$fname")) ) {
    ## Stick whatever edits into $newname
    $newname = $_;
    ## Compile a regex for testing for user bypass `p<RET>'
    $reg = $fname . "p\$";
    $cmpreg = qr/$reg/;
    ## If we don't have a `p<RET>' then rename to $newname
    if ( $newname !~ /$reg/){
    print "Renaming $fname to $_\n";
    rename $fname, $newname or warn "Can't rename $fname to $newname: $";
    ## Bump our cmdline memory
    $term->addhistory($_) if /\S/;
    }else{
    print "No rename performed on $fname, moving on..\n";
    }

    }
    }
    sub usage {
    print <<EOM;

    Purpose: Make renaming of files faster
    USAGE: \`$myscript [-d 'DIR'] [-r REG]'
    Flags: -d DIR <optional> for selecting a directory to work in
    -r REG <optional> for setting the file selection regex
    EOM
    }

    Harry Guest

Similar Threads

  1. renaming url and saving url to a file
    By moniquemac in forum Macromedia Contribute General Discussion
    Replies: 0
    Last Post: October 28th, 05:43 AM
  2. Renaming Files inside a GZ Zip File
    By Mike Flannigan in forum PERL Miscellaneous
    Replies: 6
    Last Post: August 10th, 12:49 AM
  3. need help renaming file
    By roy terrazas in forum ASP Components
    Replies: 3
    Last Post: July 31st, 04:46 AM
  4. renaming file
    By Natty Gur in forum ASP.NET General
    Replies: 0
    Last Post: July 30th, 03:13 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139