Main Page/Stuff/Ripping Karaoke CDs/cpname.pl

From phurvitz
Jump to: navigation, search

<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};

}

  1. 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; }


  1. ----------------------------( promptUser )-----------------------------#
  2. #
  3. FUNCTION: promptUser #
  4. #
  5. PURPOSE: Prompt the user for some type of input, and return the #
  6. input back to the calling program. #
  7. #
  8. ARGS: $promptString - what you want to prompt the user with #
  9. $defaultValue - (optional) a default value for the prompt #
  10. #
  11. -------------------------------------------------------------------------#

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};

  1. were args supplied?

if($sourcedir eq "" | $destdir eq "") {

   usage();

}

  1. 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;

}

  1. a new array

@titles = ();

  1. read input files

opendir(SRCDIR, "$sourcedir"); @srcfiles = readdir(SRCDIR);

  1. 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");
   }

}

  1. how many titles?

$count = @titles; if ($count==0) {

   print "No files, exiting\n";
   exit;

}

  1. 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\"";

}

  1. response

@yesArray = ("Y", "YES", "Yes", "y", "yes");

$rename = &promptUser("Rename?", "yes | no"); if (!grep {$_ eq $rename} @yesArray) {

 exit;

}

  1. 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>