Help - Search - Members - Calendar
Full Version: File rename util in perl
Hydrogenaudio Forums > Hydrogenaudio Forum > General Audio
reader
I'd like to write a file naming utility in perl, or better, be told there already is one.

I'm not skilled enough to write it myself but could be of some help maybe.
Here is what I think would be usefull.

First, the situation where this would be usefull would be when you have numerous files that don't lend them selves to mass renaming readily. That is, they don't have repeating strings or a mixed bag of extensions or just need to be redone in odd ways.

In that situation we need to slurp up a list of files to rename and then be presented with one at a time on an editable command line with something like vi or emacs editing style. (Or bash shell).

Edit the file name and hit enter. That action would rename the file seamlessly and then present the next one for editing.

The above command line style would be far faster than any clicking type interface
anyone familiar with linux or unix will recognize the value of the above approach.

I can write the preprocessing (Slurping up the files by globbing or whatever) and the seamless rename (postprocessing) in perl with a litle time. but where I'm really lost is how to create an editable command line inside a perl script.

The most primitive style of user input:
$line = <STDIN>;
PROCESS $line.

Allows no editing at all. How might I get the described behavior where the script
presents me with a filename on a command line ready to edit and allows some kind of editing inteface until the first <ENTER> is pressed?

Anyone want to work on this with me?
Kloszard
Good idea.
Masstager works best when already provided with pre-sorted filelist of uniform naming scheme. I currently lack the time, but felt the need for such an utility for a long time. Usually, when I borrow a music collection from someone, I can't help thinking that it is utterly impossible to have such a mess in the naming scheme (when will those people learn...).

So, my scenario would be a directory with 'randomly' named mpc/mp3/ogg/...
The utility would provide me with one filename at a time, displaying the name with coloured part of the name which is in scope. Processing from left to right on a word-by-word basis (separators would be punctuation marks, underscores, spaces, %20's etc. etc.) the utility would wait for input from the user (keyboard shortcuts like: [A]rtist, a[L]bum, [T]itle and so on) and concatenate the words consecutively (which does not neccesarily mean adjacent) marked as e.g. an album name to one string.

Now, there should be a possibility to set the separators. If for example one knows that the files are already named ina strange but consistent manner (not consistent enough to pass them to masstagger though) the process could be accelerated by processing the name in coarser chunks. The first and most obvious mod is not treating spaces as separators.

Anyway, much functionality can be added, but for me the number one feature should be no-mouse-operation.

So - glad as I would be to work on it I have other things really urgent to do right now. I think that somewhere I have some good materials on regular expressions and also an electronic version of a book about PERL so if anybody cares for it I can give them away. Yes I know. Google is out there. But again - I wish to contribute :)
reader
QUOTE(Kloszard @ Apr 30 2004, 02:47 AM)
Good idea.
Masstager works best when already provided with pre-sorted filelist of uniform naming scheme. I currently lack the time, but felt the need for such an utility for a long time. Usually, when I borrow a music collection from someone, I can't help thinking that it is utterly impossible to have such a mess in the naming scheme (when will those people learn...).

So, my scenario would be a directory with 'randomly' named mpc/mp3/ogg/...
The utility would provide me with one filename at a time, displaying the name with coloured part of the name which is in scope. Processing from left to right on a word-by-word basis (separators would be punctuation marks, underscores, spaces, %20's etc. etc.) the utility would wait for input from the user (keyboard shortcuts like: [A]rtist, a[L]bum, [T]itle  and so on) and concatenate the words consecutively (which does not neccesarily mean adjacent) marked as e.g. an album name to one string.

Now, there should be a possibility to set the separators. If for example one knows that the files are already named ina strange but consistent manner (not consistent enough to pass them to masstagger though) the process could be accelerated by processing the name in coarser chunks. The first and most obvious mod is not treating spaces as separators.

Anyway, much functionality can be added, but for me the number one feature should be no-mouse-operation.

So - glad as I would be to work on it I have other things really urgent to do right now. I think that somewhere I have some good materials on regular expressions and also an electronic version of a book about PERL so if anybody cares for it I can give them away. Yes I know. Google is out there. But again - I wish to contribute :)

I've got a first draft of a very basic renaming tool.
It passes you a file name from a list gathered in the first part and allows some
Fairly sophisticated line editing with line memory to help retrieve recurring parts.
Unix or linux users will recognize the cmdline style

Its in perl and should work on windows platform if you adjust the first line to be the address of your installed perl interpreter.
(Google for `Active perl'... Its a good distribution and easy to install)

Once you've copy and pasted this script, type `rena.pl help'
for a brief usage message. But basically it works like this:

Give the script a directory name to work in with -d flag
And a regular expression to find files with using the -r flag

`rena.pl -d 'SOMEDIR' -r '\.wav$'

Neither is required if you cd to the working dir and don't mind having to type a letter `p' and hit ENTER to bypass files your not interested in. Then it would just be:
rena.pl

Copy a directory full of files to rename to a tmp location and try this out before you try it on non-backedup files.

The script (Beware of line wrapping from mailing)
#!/usr/local/bin/perl -w

# Keywords: rena.pl - designed to hasten file renaming [ File names
# presented one at a time for editing]
# 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
}
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2008 Invision Power Services, Inc.