publicscripts/mvwrap/mvwrap

81 lines
1.8 KiB
Text
Raw Normal View History

2001-07-31 08:40:13 +00:00
#!/usr/bin/perl -w
#
2001-07-31 08:55:40 +00:00
# Scriptje om een editor op de inhoud van directories los te laten.
# Makkelijker dan 6000 keer move met de hand doen.
2001-07-31 08:40:13 +00:00
# (C) 2001 Ward Wouts
#
use IO::File;
use File::Copy;
use POSIX qw(tmpnam);
$DEBUG = 0;
#Read current dir
opendir(DIRHANDLE, ".") or die "couldn't open .: $!";
while ( defined ($filename = readdir(DIRHANDLE)) ) {
if ($DEBUG) { print "Inside . is something called $filename\n"; }
push @dir, "$filename\n";
}
closedir(DIRHANDLE);
@source= sort @dir;
if ($DEBUG) { print @source; }
# make tempfiles and install handler to remove them
do { $target_name = tmpnam() }
until $target = IO::File->new($target_name, O_RDWR|O_CREAT|O_EXCL);
END { unlink($target_name) or die "Couldn't unlink $target_name: $!" }
foreach (@source) {
print $target $_;
}
close ($target);
2001-07-31 08:55:40 +00:00
$editor = defined $ENV{EDITOR} ? $ENV{EDITOR} : "vi";
@editor = ("$editor", "$target_name");
system(@editor) == 0
or die "System @editor failed: $?";
2001-07-31 08:40:13 +00:00
open $target, $target_name;
while (<$target>) {
push @target, $_;
}
close ($target);
if ($DEBUG) { print @target; }
unless ( scalar(@source) == scalar(@target) ) {
die "Aborting. Source and target list don't have the same number of lines.\n";
}
if (&check_unique(@target)) {
die "Aborting. You're trying to move multiple files to the same name.\n";
2001-07-31 08:40:13 +00:00
}
$i=0;
while ( $i < scalar(@source) ) {
$source=$source[$i];
chomp($source);
$target=$target[$i];
chomp($target);
unless ( $source eq $target ) {
if ($DEBUG) { print "mv $source $target\n"; }
move("$source", "$target")
or die "move failed: $!";
}
$i++;
}
# returns 0 if all entries in @target_list are unique. 1 if not.
sub check_unique (@target_list){
my(@target_list, %seen, @uniqu);
@target_list = @_;
%seen = ();
@uniqu = grep { ! $seen{$_} ++ } @target_list;
unless (scalar(@uniqu) == scalar(@target) ) {
return 1;
}
return 0;
}