| 1 | use strict; | ||
| 2 | use warnings; | ||
| 3 | use File::Copy qw( move ); | ||
| 4 | use File::Spec; | ||
| 5 | use File::Basename qw( basename ); | ||
| 6 | |||
| 7 | # very basic and probably incomplete implementation of | ||
| 8 | # UNIX mv written in perl to be used on MSWin32 if it | ||
| 9 | # can't be found in the path. | ||
| 10 | |||
| 11 | my $interactive = 0; | ||
| 12 | |||
| 13 | while($ARGV[0] && $ARGV[0] =~ /^-/) | ||
| 14 | { | ||
| 15 | my $arg = shift @ARGV; | ||
| 16 | last if $arg eq '--'; | ||
| 17 | if($arg =~ /^--/) | ||
| 18 | { | ||
| 19 | print STDERR "unknown switch: $arg\n"; | ||
| 20 | exit 1; | ||
| 21 | } | ||
| 22 | my @switches = split //, $arg; | ||
| 23 | shift @switches; | ||
| 24 | foreach my $switch (@switches) | ||
| 25 | { | ||
| 26 | if($switch eq 'i') | ||
| 27 | { $interactive = 1 } | ||
| 28 | elsif($switch eq 'f') | ||
| 29 | { $interactive = 0 } | ||
| 30 | else | ||
| 31 | { | ||
| 32 | print STDERR "unknown switch: -$switch\n"; | ||
| 33 | } | ||
| 34 | } | ||
| 35 | } | ||
| 36 | |||
| 37 | my $dest = pop @ARGV; | ||
| 38 | my @src = @ARGV; | ||
| 39 | |||
| 40 | unless(defined $dest && @src > 0) | ||
| 41 | { | ||
| 42 | print STDERR "usage: $0 [-i] [-f] source1 [ source2 [ ... ] ] dest\n"; | ||
| 43 | exit 1; | ||
| 44 | } | ||
| 45 | |||
| 46 | if(@src > 1 && ! -d $dest) | ||
| 47 | { | ||
| 48 | print STDERR "if multiple source arguments are given then the destination must be a directory\n"; | ||
| 49 | exit 1; | ||
| 50 | } | ||
| 51 | |||
| 52 | foreach my $src (@src) | ||
| 53 | { | ||
| 54 | my $fn_dest = $dest; | ||
| 55 | $fn_dest = File::Spec->catfile($dest, basename $src) if -d $dest; | ||
| 56 | if($interactive && -e $fn_dest) | ||
| 57 | { | ||
| 58 | print "mv: Overwrite '$fn_dest'? "; | ||
| 59 | my $answer = <STDIN>; | ||
| 60 | next unless $answer =~ /^y/i; | ||
| 61 | } | ||
| 62 | move($src, $fn_dest) or die "move failed: $!"; | ||
| 63 | } |