#!/usr/bin/perl
#
# Prabakar 1/17/08
#
# This script file has been customized for Windows 32
#
# This perl script renumbers the files present in the given directory
#

use strict;

if ($#ARGV != 0) { # in Perl, argcnt is zero for one argument
    die "Usage: s:\perl\bin\perl.exe $0 srcImageDir \n";
}


my $dirName = $ARGV[0];

#$f =~ s#'#\\'#g; # prefix meta char for any apostraphies
my @files = `dir/B/ON $dirName`;
# DIR/B lists files as well as folders one per line without summary
# DIR/ON lists files in ascending order by name

chdir($dirName) or die "Unable to enter dir $dirName:$!\n";

my $fileCount = 1;
my $newCount = "";
my $thousandDigit = 0;
my $hundredDigit = 0;
my $tenDigit = 0;
my $oneDigit = 0;
# foreach works in DOS
foreach my $file (@files) {
  $file =~ s/\s*$//;
  if (($file =~ /[0-9]{4}\./oi) && ($fileCount < 10000)) {
    $oneDigit = $fileCount % 10;
    $tenDigit = ($fileCount / 10) % 10;
    $hundredDigit = ($fileCount / 100) % 10;
    $thousandDigit = ($fileCount / 1000) % 10;
    $newCount = $thousandDigit . $hundredDigit . $tenDigit . $oneDigit . ".";
    my $newfile = $file;
    $newfile =~ s/[0-9]{4}\./$newCount/;
    if ($file ne $newfile) {
	#print "rename $file $newfile\n";
    	system("rename $file $newfile ");
    }
    $fileCount++;
  }
}
$fileCount--;
chdir("..");
print "\n In directory $dirName, $fileCount files have been renumbered \n\n";

exit(0);
