added options:
-h for help -e "editor" for anternative editor -s "streameditor" for stream editors also added checking for empty lines
This commit is contained in:
parent
b0b7aeeebb
commit
053bdc1b09
1 changed files with 80 additions and 13 deletions
|
|
@ -12,6 +12,9 @@
|
||||||
use IO::File;
|
use IO::File;
|
||||||
use File::Copy;
|
use File::Copy;
|
||||||
use POSIX qw(tmpnam);
|
use POSIX qw(tmpnam);
|
||||||
|
use Getopt::Std;
|
||||||
|
|
||||||
|
&cmdline;
|
||||||
|
|
||||||
#Read current dir
|
#Read current dir
|
||||||
opendir(DIRHANDLE, ".") or die "couldn't open .: $!";
|
opendir(DIRHANDLE, ".") or die "couldn't open .: $!";
|
||||||
|
|
@ -21,26 +24,25 @@ while ( defined ($filename = readdir(DIRHANDLE)) ) {
|
||||||
closedir(DIRHANDLE);
|
closedir(DIRHANDLE);
|
||||||
@source = sort @dir;
|
@source = sort @dir;
|
||||||
|
|
||||||
|
&lock;
|
||||||
|
|
||||||
# make tempfiles and install handler to remove them
|
# make tempfiles and install handler to remove them
|
||||||
do { $target_name = tmpnam() }
|
do { $target_name = tmpnam() }
|
||||||
until $target = IO::File->new($target_name, O_RDWR|O_CREAT|O_EXCL);
|
until $target = IO::File->new($target_name, O_RDWR|O_CREAT|O_EXCL);
|
||||||
END { unlink($target_name) or die "Couldn't unlink $target_name: $!" }
|
END { unlink($target_name) or die "Couldn't unlink $target_name: $!";
|
||||||
|
unlink(".mv_wrap") or die "Couldn't unlink .mv_wrap: $!"; }
|
||||||
|
|
||||||
foreach (@source) {
|
foreach (@source) {
|
||||||
print $target $_;
|
print $target $_;
|
||||||
}
|
}
|
||||||
close ($target);
|
close ($target);
|
||||||
|
|
||||||
if ( -e ".mv_wrap") {
|
|
||||||
die "Another mv_wrap process is active in this direcory\n"
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
open LOCK, ">.mv_wrap";
|
|
||||||
print LOCK $$;
|
|
||||||
close LOCK;
|
|
||||||
}
|
|
||||||
|
|
||||||
&edit($target_name);
|
if ($option{s}) {
|
||||||
|
&stream_edit($target_name);
|
||||||
|
} else {
|
||||||
|
&edit($target_name);
|
||||||
|
}
|
||||||
|
|
||||||
open $target, $target_name or die "Couldn't open tempfile: $target_name: $!";
|
open $target, $target_name or die "Couldn't open tempfile: $target_name: $!";
|
||||||
@target = <$target>;
|
@target = <$target>;
|
||||||
|
|
@ -50,6 +52,11 @@ unless ( scalar(@source) == scalar(@target) ) {
|
||||||
die "Aborting. Source and target list don't have the same number of lines.\n";
|
die "Aborting. Source and target list don't have the same number of lines.\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# ook nog checken op lege regels hier....
|
||||||
|
foreach $line (@target) {
|
||||||
|
if ( $line =~ m/^$/ ) { die "Aborting. You can't move to empy names.\n"; }
|
||||||
|
}
|
||||||
|
|
||||||
if ( &check_unique(@target) ) {
|
if ( &check_unique(@target) ) {
|
||||||
die "Aborting. You're trying to move multiple files to the same name.\n";
|
die "Aborting. You're trying to move multiple files to the same name.\n";
|
||||||
}
|
}
|
||||||
|
|
@ -63,16 +70,38 @@ if (@unsafe = &check_safety(\@source, \@target)) {
|
||||||
# final move
|
# final move
|
||||||
&move_files(\@source, \@target);
|
&move_files(\@source, \@target);
|
||||||
|
|
||||||
unlink ".mv_wrap" or die "Couldn't remove lock";
|
|
||||||
|
|
||||||
# call EDITOR or vi with filename
|
# call EDITOR or vi with filename
|
||||||
# edit($filename);
|
# edit($filename);
|
||||||
sub edit {
|
sub edit {
|
||||||
@editor = (defined $ENV{EDITOR} ? $ENV{EDITOR} : "vi", shift);
|
my $filename = shift;
|
||||||
|
@editor = (defined $ENV{EDITOR} ? $ENV{EDITOR} : "vi", "$filename");
|
||||||
|
if ($option{e}) { @editor = ($option{e}, "$filename") }
|
||||||
system(@editor) == 0
|
system(@editor) == 0
|
||||||
or die "System @editor failed: $?";
|
or die "System @editor failed: $?";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sub stream_edit {
|
||||||
|
my $filename = shift;
|
||||||
|
my ($pid, @output);
|
||||||
|
@editor = (split(" ", $option{s}), "$filename");
|
||||||
|
# foreach (@editor) { print "$_\n"; }
|
||||||
|
die "cannot fork: $!" unless defined ($pid = open(KID, "-|"));
|
||||||
|
if ($pid) { #parent
|
||||||
|
while (<KID>) {
|
||||||
|
# print ;
|
||||||
|
push @output, $_;
|
||||||
|
}
|
||||||
|
close(KID);
|
||||||
|
} else { #child
|
||||||
|
exec(@editor) or die "can't exec program: $!\n";
|
||||||
|
}
|
||||||
|
open OUT, ">$filename" or die "Can't open $filename for writeing: $!\n";
|
||||||
|
foreach $line (@output) {
|
||||||
|
print OUT "$line";
|
||||||
|
}
|
||||||
|
close OUT;
|
||||||
|
}
|
||||||
|
|
||||||
# moves files from the names in one array to the names in another array
|
# moves files from the names in one array to the names in another array
|
||||||
# must be called like:
|
# must be called like:
|
||||||
# move_files(\@source, \@target)
|
# move_files(\@source, \@target)
|
||||||
|
|
@ -142,3 +171,41 @@ sub safety_belt {
|
||||||
$source->[$filenr] = "$rand\n";
|
$source->[$filenr] = "$rand\n";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sub lock {
|
||||||
|
if ( -e ".mv_wrap") {
|
||||||
|
die "Another mv_wrap process is active in this direcory\n"
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
open LOCK, ">.mv_wrap";
|
||||||
|
print LOCK $$;
|
||||||
|
close LOCK;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
sub cmdline {
|
||||||
|
%option = ();
|
||||||
|
getopts("e:hs:", \%option);
|
||||||
|
|
||||||
|
&help if $option{h};
|
||||||
|
}
|
||||||
|
|
||||||
|
sub help {
|
||||||
|
print << "EOT";
|
||||||
|
|
||||||
|
This is a little script to make renaming large quantities of files easy.
|
||||||
|
It basically lets you edit the names in a directory with an editor
|
||||||
|
of your choice. By default this will be "vi", but it honors the EDITOR
|
||||||
|
environment variable. Which in turn can be overridden with the -e option.
|
||||||
|
|
||||||
|
At the moment it takes the following options:
|
||||||
|
-h this help message
|
||||||
|
-e "editor" invoke with this editor
|
||||||
|
-s "streameditor" use a stream editor
|
||||||
|
this makes things like:
|
||||||
|
mv_wrap.pl -s "sed -e s/1/5/"
|
||||||
|
possible
|
||||||
|
|
||||||
|
EOT
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue