MacTech Article Code.


#!perl

for($ii=0;$ii<=$#ARGV; $ii++) { 
  if(!do_a_folder($ARGV[$ii])){exit;}
  }


sub do_a_folder{
$object=shift(@_);    
if( -f $object) {   # -f checks if $object is a file, 
                    # if it is, get enclosing folder.
  $x=rindex($object,':');   # find LAST occurrence of :
  $object=substr($object,0,$x);   # remove last part of
                                  # path name
  }
# $object now path name to folder
$x=rindex($object,':');   # find LAST occurrence of :
$fold_name=substr($object,$x+1);  # get name of folder
if( -d $object) {                 # it's a folder
  unlink("$object:MAVICA.HTM");  
    # This deletes a junk file which often gets copied.
  return process_folder($object,$fold_name);
  }
  # else quietly do nothing.
return 1;
}


sub process_folder{
$fold=shift(@_);
$fold_name=shift(@_);
# Make sure names wcan't be too long for the Finder.
$fold_name=substr($fold_name,0,23);
undef(@fold_name_files);  # Clear old values
undef(@fold_name_files);  # Clear old values
if( opendir(DIR,$fold)) {   # if we can read the directory
  chdir($fold);             # change the working directory
  @files=readdir(DIR);      # read all objects into an array
  closedir(DIR);            # close the directory for reading
  for($i=0;$i<=$#files;$i++) {
    if( $files[$i]=~m/^$fold_name\d*\.jpg$/){
        # remember the folder_name files
      push(@fold_name_files,$files[$i]);
      }
    elsif( $files[$i]=~m/^MVC-\d*L\.JPG$/) {  
        # remember the MVC files
      push(@mvc_files,$files[$i]);
      }
    }  
  if($#mvc_files<0 && $#fold_name_files<0) {
    return 1; # Nothing to do.
    }
  else {  # Go rename the files.
  return ( 
    setup_rename(\@mvc_files,\@fold_name_files,$fold_name));
    }
  }
else { print"Failed to open $fold\n"; return 0;}  
}


sub setup_rename{
$mvc_files=shift(@_);
$fold_name_files=shift(@_);
$fold_name=shift(@_);
$startNumber=1;   # The first file is numbered 1.
#
$new_digit_size=length(
      $#$fold_name_files+$#$mvc_files+1+$startNumber);
if($new_digit_size>4){
  print"More than 9,999 files? No way!\n";
  return 1;   # Will process other folders 
  }
#
# Get MVC creation time (if possible).
if( ($#$fold_name_files>=0) ) {
  $time_MVC=(stat($$mvc_files[0]))[10];
  }  
# Get folder_name creation time (if possible).
if($#$fold_name_files>=0) {
  $time_FN=(stat($$fold_name_files[0]))[10];
  }
# Calculate starting numbers.
if($#$mvc_files<0) {Ê$fold_name_startNumber=$startNumber;}
elsif($#$fold_name_files<0) {$mvc_startNumber=$startNumber;}
elsif($time_MVC<$time_FN) {
  $mvc_startNumber=$startNumber;
  $fold_name_startNumber=$#$mvc_files+1+$startNumber;
  }
else {
  $mvc_startNumber=$#$fold_name_files+1+$startNumber;
  $fold_name_startNumber=$startNumber;
  }
return rename_files($mvc_files,$mvc_startNumber,
    $fold_name_files,$fold_name_startNumber,
    $fold_name,$new_digit_size);
}


sub rename_files{
# Make temporary folder - the name will be a number
$dir=0;
while( -d $dir || -f $dir ) {$dir++;}
  # Possible infinite loop - but need thousands of 
  # folders/files with numbers as names.Don't worry.
if(!mkdir($dir,0777)) {
  print"Failed to make temporary folder.\n";
  return 0;
  }
($filesA,$startA,$filesB,$startB,$prefix,$digit_size)=@_;
$dir_prefix=":$dir:$prefix";
# Move the first batch of files, then the second.
# Bail if error. 
if(!mv_tmp($startA,$filesA,$dir_prefix,$digit_size)){
  return 0;
  }
if(!mv_tmp($startB,$filesB,$dir_prefix,$digit_size)){
  return 0;
  }
# move the files back. Bail if error.
if(!mv_back($dir)){return 0;}
# Delete the temporary directory
return rmdir($dir);
}


sub mv_tmp{
($first,$list,$dir_prefix,$digitSize)=@_;
foreach $h (@$list) {
  $numStr=substr("00000",0,$digitSize-length($first)).$first;
  if(!rename($h,"$dir_prefix$numStr.jpg") ){
    print"Failed to move $h into $dir\n";
    return 0;
    }
    $first++;
  }
return 1;
}


sub mv_back{
$dir=shift(@_);
if(opendir(DIR,$dir) ){
  @files=readdir(DIR);  # read all objects into an array
  closedir(DIR);        # close the directory for reading
  chdir($dir);
  foreach $h (@files) {
    if(!rename($h,"::$h") ){
      print"Failed to move $h out of $dir\n";
      return 0;
      }
    }
  chdir("::");
  return 1;
  }
else {return 0;}
}