CoderZone.org

Category: >> Perl Code >> Rename Files Interactively Bookmark and Share

<< lastnext >>

Snippet Name: Rename Files Interactively

Description: Very handy way to step through files and rename them.

Also see:
» Perl version of file_get_contents&...
» Read the contents of an entire fil...
» Create an array from the contents ...
» Create an array from the lines of ...
» Remove Duplicate Lines from a File
» Sort A File List By Month Name In ...
» Get A Directory Listing Of Specifi...
» Check If A Directory Allows File C...

Comment: (none)

Author: CoderZone
Language: PERL
Highlight Mode: PERL
Last Modified: December 03rd, 2010

#!/usr/bin/perl
 
use warnings;
use strict;
 
use File::Glob qw(:globally :nocase);
use File::Copy;
 
my $editor = '/bin/vi';
 
my @files = @ARGV;
 
die "Usage: $0 FILES\n" unless ($#files >= 0);
 
print join( $/, @files ), $/;
 
for my $file (@files) {
 
    my $tmp = '/tmp/ren.' . $$;
 
    die "Can't write to '$tmp': $!\n" unless open( TMP, ">$tmp" );
 
    print TMP "$file";
 
    close TMP;
 
    system("$editor $tmp");
 
    die "Can't read '$tmp': $!\n" unless open( TMP, $tmp );
 
    chomp( my $new = <TMP> );
 
    close TMP;
 
    if ( $new eq $file ) {
 
        print "'$file': No change.\n";
 
    }
    elsif ( -f $new ) {
 
        print "'$new': Already exists.\n";
 
    }
    else {
 
        die "Can't rename '$file' to '$new': $!\n" unless move( $file, $new );
 
        print "mv '$file' -> '$new'\n";
    }
 
    unlink $tmp;
 
}
 
 
There haven't been any comments added for this snippet yet. You may add one if you like.  Add a comment 
© coderzone.org | users online: 8