« audio has frames too ii | Main | ds ii »

February 1, 2009

no more musical chairs

i forgot to put the segments of the rosenkreuzstilette run in proper order before making the verification-stage avisynth scripts: segment 8 was actually segment 5, and segments 5-7 all had to move up one. it sounds simple enough, but there is no way to rename everything in situations like this without having at least one file have a temporary name. and if it's me doing the renaming i will often second-guess what i am doing halfway through the operation because i am ocd like that.

so i cooked up a quick perl script to just give every file in the directory a temporary name and then rename everything back based on a transformation rule. the old segment numbers are the keys of %rehash and the values are the new numbers. i use %files to remember what the names used to be after the files are given their temporary names. i made the temporary names random (with a check to make sure they're unused) since i may have more than one copy of the same file in the directory (so renaming them to their md5 hash or something is out).

enjoy.

#!/usr/bin/perl -w
#nathan jahnke 

my $natename = 'rks';
my $thedir = "/p/${natename}";
my %rehash = ( #old, new
	8=>5,
	5=>6,
	6=>7,
	7=>8,
);
my %files;

opendir(P,$thedir);
my @files = readdir(P);
closedir(P);

for (@files) {
	if (-f "${thedir}/${_}") {
		my $newname = &newname;
		system('mv', '-nv', "${thedir}/${_}", "${thedir}/${newname}");
		$files{$newname} = $_;
	}
}

for (keys %files) {
	my $newname;
	if ($files{$_} =~ m/^${natename}-v-(\d+)\.(.+)$/) {
		my $segnum;
		$segnum = $1;
		$segnum = $rehash{$segnum} if $rehash{$segnum};
		$newname = "${natename}-v-${segnum}.${2}";
	} else {
		$newname = "${files{$_}}";
	}
	system('mv', '-nv', "${thedir}/${_}", "${thedir}/${newname}");
}

exit 0;


sub newname {
	my $retvar = '';
	while (-e "${thedir}/".$retvar) { #try until we get a unused name
		$retvar = &random;
	}
	return $retvar;
}

sub random {
	my $retvar = rand(1);
	$retvar =~ s/^0\.//;
	return $retvar;
}

Posted by njahnke at February 1, 2009 6:13 PM

Comments

Post a comment

Thanks for signing in, . Now you can comment. (sign out)

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)


Remember me?