CCL Home Page
Up Directory CCL change_max.pl
#!/usr/bin/perl

if ($#ARGV == 0) {
    $n = 0;
    $dir = ".";
    $file = $ARGV[0];
}
elsif ($#ARGV == 1) {
    if($ARGV[0] =~ /^\d+$/) {
	$n = $ARGV[0];
	$dir = ".";
    }
    else {
	$dir = $ARGV[0];
	$n = 0;
    }
    $file = $ARGV[1];
}
elsif ($#ARGV == 2) {
    if($ARGV[0] =~ /^\d+$/) {
	$n = $ARGV[0];
	$dir = $ARGV[1];
    }
    else {
	$dir = $ARGV[1];
	$n = $ARGV[0];
    }
    $file = $ARGV[2];
}
else {
    die "Usage: change_maxnum.pl [dir] [n] nameNNNN.html\n" .
"  Changes maxnum to n in files:  nameNNNN.html,  name(NNNN+1).html...\n" .
"  If n not given, takes the highest number of the file name.\n" .
"  If dir not given, current directory is assumed.\n";
}

if($file =~ /^([a-z]+)(\d+)\.html$/) {
    $fname = $1;
    $fnumber = $2;
}
else {
    die "cannot parse file name $file";
}

$numlen = length($fnumber);
$format = "%0" . $numlen . "d"; 

opendir(DIR, $dir) || die "Cannot open directory $dir for reading\n";

$maxnum = 0;

while($entry = readdir(DIR)) {
    if($entry =~ /^$fname(\d+)\.html$/ ) {
	print STDOUT "$entry\n";
	if($1 > $maxnum) {
	    $maxnum = $1;
	}
    }
}

if($n == 0) {
    $n = sprintf("%d", $maxnum);
}

print STDOUT "new maxnum is $n\n";

for ($i = $fnumber; $i <= $maxnum; $i++) {
    $filename = $dir . "/" . $fname . sprintf($format, $i) . '.html';
    print STDOUT "File: $filename\n";
    $temp = "/tmp/junk-$$";
    if(-e $filename) {
	open(FIL, "<$filename") || die "cannot open $filename\n";
	open(TEMP, ">$temp") || die "cannot open $temp\n";
	while($line = ) {
	    if($line =~ /^(var\s*maxnum\s*=\s*)(\d+)([^0-9].*)$/) {
		$line = $1 . $n . $3 . "\n";
		print STDOUT "changed to:\n$line";
	    }
	    print TEMP $line;
	}
	close(FIL);
	close(TEMP);
	system("mv $temp $filename");
	print STDOUT "Moved file $temp, to file $filename\n";
    }
    else {
	print STDOUT "Missing file $filename\n";
    }
}
Modified: Wed Feb 13 07:46:31 2002 GMT
Page accessed 2674 times since Mon Feb 3 15:32:48 2003 GMT