Main Page/Stuff/Ripping Karaoke CDs/cpname.pl
From phurvitz
< Main Page | Stuff | Ripping Karaoke CDs
<source lang=perl> use vars qw/ %opt /; use File::Basename;
sub init() {
use Getopt::Std; my $opt_string = 'hd:s:'; getopts( "$opt_string", \%opt ) or usage(); usage() if $opt{h};
}
- Message about this program and how to use it
sub usage() {
print STDERR << "EOF";
# srcdir contains files with good names but bad size # (from Power CD+G burner trialware) # destdir contains files good files with bad names # (from MP3+G Toolz) # rip first using MP3+G Toolz to a set of bin files # rip using PowerCD+G to get bin files with names # use this script to copy names from PowerCD+G to MP3+G # then rip MP3+G files to Audio + G usage: $0 [-h] [-s srcdir] [-d destdir] -h : this (help) message -s : source directory -d : destination directory example: $0 -s c:/temp/karaoke/1 -d c:/temp/karaoke/2
EOF exit; }
- ----------------------------( promptUser )-----------------------------#
- #
- FUNCTION: promptUser #
- #
- PURPOSE: Prompt the user for some type of input, and return the #
- input back to the calling program. #
- #
- ARGS: $promptString - what you want to prompt the user with #
- $defaultValue - (optional) a default value for the prompt #
- #
- -------------------------------------------------------------------------#
sub promptUser {
#-------------------------------------------------------------------# # two possible input arguments - $promptString, and $defaultValue # # make the input arguments local variables. # #-------------------------------------------------------------------#
local($promptString,$defaultValue) = @_;
#-------------------------------------------------------------------# # if there is a default value, use the first print statement; if # # no default is provided, print the second string. # #-------------------------------------------------------------------#
if ($defaultValue) { print $promptString, "[", $defaultValue, "]: "; } else { print $promptString, ": "; }
$| = 1; # force a flush after our print $_ = <STDIN>; # get the input from STDIN (presumably the keyboard)
#------------------------------------------------------------------# # remove the newline character from the end of the input the user # # gave us. # #------------------------------------------------------------------#
chomp;
#-----------------------------------------------------------------# # if we had a $default value, and the user gave us input, then # # return the input; if we had a default, and they gave us no # # no input, return the $defaultValue. # # # # if we did not have a default value, then just return whatever # # the user gave us. if they just hit the <enter> key, # # the calling routine will have to deal with that. # #-----------------------------------------------------------------#
if ("$defaultValue") { return $_ ? $_ : $defaultValue; # return $_ if it has a value } else { return $_; }
}
init();
$sourcedir = $opt{s}; $destdir = $opt{d};
- were args supplied?
if($sourcedir eq "" | $destdir eq "") {
usage();
}
- do the dirs exist?
if (!-d $sourcedir) {
print "source dir does not exist\n"; exit;
} if (!-d $destdir) {
print "dest dir does not exist\n"; exit;
}
- a new array
@titles = ();
- read input files
opendir(SRCDIR, "$sourcedir"); @srcfiles = readdir(SRCDIR);
- if the filename is "*.bin" then push it to a new array
foreach $f (@srcfiles) {
my ($name,$dir,$ext) = fileparse($f,qr/\.[^.]*/); if ($ext eq ".bin") { #print "$name $dir $ext\n"; push(@titles, "$f"); }
}
- how many titles?
$count = @titles; if ($count==0) {
print "No files, exiting\n"; exit;
}
- keep processing to construct the list of renaming
@destfiles = `ls \"$destdir\"`; for ($i = 0; $i < $count; $i++) {
$sf = ($titles[$i]); $df = ($destfiles[$i]); chomp $sf; chomp $df; $df = join("\\", $destdir, $df); $sf = join("\\", $destdir, $sf); print "old name: \"$df\" \n new name: \"$sf\"\n"; #system "mv \"$df\" \"$sf\"";
}
- response
@yesArray = ("Y", "YES", "Yes", "y", "yes");
$rename = &promptUser("Rename?", "yes | no"); if (!grep {$_ eq $rename} @yesArray) {
exit;
}
- keep processing to rename
@destfiles = `ls \"$destdir\"`; for ($i = 0; $i < $count; $i++) {
$sf = ($titles[$i]); $df = ($destfiles[$i]); chomp $sf; chomp $df; $df = join("\\", $destdir, $df); $sf = join("\\", $destdir, $sf); rename $df, $sf;
} </source>