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

$Usage = "Usage: onup.pl [dir] [n] nameNNNN.html\n" .
        "Shift all slides n positions up starting at the given one.\n" . 
        "It will create a hole with n empty slots:\n" . 
        "      nameNNNN.html, name(NNNN+1).html, ... name(NNNN+n-1).html\n";
 
if($#ARGV < 0) {
    die $Usage;
}
elsif ($#ARGV == 0) {
    $dir = ".";
    $n = 1;
    $file = $ARGV[0];
}
elsif ($#ARGV == 1) {
    if($ARGV[0] =~ /^(\d+)$/) {
	$n = $1;
	$dir = ".";
    }
    else {
	$n = 1;
	$dir = $ARGV[0];
    }
    $file = $ARGV[1];
}
elsif ($#ARGV == 2) {
    if($ARGV[0] =~ /^(\d+)$/) {
	$n = $1;
	$dir = $ARGV[1];
    }
    elsif ($ARGV[1] =~ /^(\d+)$/) {
	$n = $ARGV[1];
	$dir = $ARGV[0];
    }
    else {
	die $Usage;
    }
    $file = $ARGV[2];
}


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

for ($i = $maxnum; $i >= $fnumber; $i--) {
    $from = $dir . "/" . $fname . sprintf($format, $i) . '.html';
    $to = $dir . "/" . $fname . sprintf($format, $i+$n) . '.html';
    if(! -e $from) {
	print STDERR "Warning!!! File $from does not exist\n";
    }
    else {
	system("mv $from $to");
	print STDOUT "Moved file $from, to file $to\n";
    }
}


Modified: Tue Feb 12 14:58:12 2002 GMT
Page accessed 3259 times since Tue Feb 4 17:28:55 2003 GMT