randomfox (randomfox) wrote,
randomfox
randomfox

Rename to a numbered file name

This Perl script renames buff-out.ogf to b###.ogf, where ### is the next higher number after the highest numbered b###.ogf file. It is used for keeping an archive of submitted distributed.net OGR packets.


#!perl -w
use strict;
require 5.004;

use DirHandle;

my $TEMPDIR = 'c:\\temp';

my $dh = new DirHandle $TEMPDIR;
defined $dh or die "Error opening directory $TEMPDIR: $!\n";

my $highnum = 0;
while (defined($_ = $dh->read)) {
    if (/b(\d+).ogf/i) {
	$1 > $highnum and $highnum = $1;
    }
}

my $nextindex = $highnum + 1;
print "Highest = $highnum, Next index = $nextindex\n";

rename "$TEMPDIR\\buff-out.ogf", "$TEMPDIR\\b$nextindex.ogf" 
    or die "Error renaming file: $!\n";

__END__

Subscribe
  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 0 comments