Learning Perl ... Randal et al: exercise: 5, chap12. rename is your friend. You could also use File::Base - holy ones would be able to ratify this better :-).
I had written the following code as a solution to the exercise... the code requires that you work from your dir1 (i.e. the folder in which you have all the files to be moved) - it is buggy - please be warned (as I'd written this as a n00b and haven't had time to debug it since).
#!/usr/bin/perl
use strict;
use warnings;
if ($#ARGV > 1) {
print "ARGV greater than 1\n";
unless (-e $ARGV[$#ARGV]) {
mkdir $ARGV[$#ARGV], 0755 or die "Cannot create directory:
+ $!";
}
my $dir = $ARGV[$#ARGV];
my $last=$#ARGV-1;
foreach (0..$last) {
if (-f $ARGV[$_]) {
if (-e "$ARGV[$#ARGV]/$ARGV[$_]") {
print "$ARGV[$#ARGV]/$ARGV[$_]\n";
print "File $ARGV[$_] already exists in $AR
+GV[$#ARGV]\n";
print "Overwrite?:(y/n)\n";
chomp (my $line = <STDIN>);
next if ($line =~ /^(\s.*|n)/i);
}
my $old = "$ARGV[$_]";
my $new = "$dir/$ARGV[$_]";
rename $old, $new;
}
}
}
if ($#ARGV == 1) {
print "ARGV equal to 1\n";
if (-f $ARGV[0]) {
rename $ARGV[0], $ARGV[1];
}
if (-d $ARGV[0]) {
unless (-e $ARGV[1]) {
mkdir $ARGV[1], 0755 or die "Cannot create directory: $!",
}
if (-e "$ARGV[1]/$ARGV[0]") {
print "File $ARGV[0] already exists in $ARGV[1]\n";
print "Overwrite?:(y/n)\n";
chomp (my $line = <STDIN>);
last if ($line =~ /^(\s.*|n)/i);
}
foreach my $file (glob "$ARGV[0]/*") {
my $oldfile = $file;
$file =~ s/$ARGV[0]/$ARGV[1]/;
rename $oldfile, $file;
}
}
}
if ($#ARGV < 1) {
print "ARGV less than 1\n";
print "There should be atleast two arguments for this to work\n";
last;
}
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.