Actually Rob your tips are very handy...
I have a nasty habit of posting only part of the code when
asking questions here, gotta break that habit.
> use strict; # always
> use warnings; # usually
Doing those :)
> - What's this 'move' thing? Have you sneakily added 'use File::Copy without
> telling us? If so, then
You guessed it
> - Do you want existing files of the same name overwritten?
Yes
So now my code looks like this:
use strict;
use warnings;
use File::Copy;
my $reportsdir = '/usr2/reports';
my $oldreportsdir = '/usr2/oldreports';
die "No destination directory" unless -d $oldreportsdir;
opendir(DIR, $reportsdir) or die "can't opendir $reportsdir: $!";
my @files = grep -f , readdir(DIR);
foreach (@files) {
move("$reportsdir/$file", $oldreportsdir)
or die "move failed: $!";
}
closedir(DIR);
Thanks for the help - mainly I want to make sure my code is
"sane" and refined. Your reply was very useful.
Tony
-----Original Message-----
From: Rob Dixon [mailto:[EMAIL PROTECTED]
Sent: Wednesday, July 02, 2003 11:57 AM
To: [EMAIL PROTECTED]
Subject: Re: File move
Hi Anthony.
Anthony Akens wrote:
> Just want to check and make sure this snippet of code will do what I think it will.
>
> Trying to copy all files from $reportsdir to $oldreportsdir
Well you shouldn't be asking us, as we can make guesses - usually
Very Good Guesses - as to whether it will work, but we are not your
computer and it may not agree with us.
use strict; # always
use warnings; # usually
> my $reportsdir = '/usr2/reports';
> my $oldreportsdir = '/usr2/oldreports';
>
> # Move everything from the report directory to the old report directory
> opendir(DIR, $reportsdir) or die "can't opendir $reportsdir: $!";
- You're checking that the 'opendir' works on $reportsdir. How about checking
that $oldreportsdir exists?
die "No destination directory" unless -d $oldreportsdir;
- You may prefer to
chdir $reportsdir;
first, so that you don't have to assemble the fully-qualified source file name.
> while (defined($file = readdir(DIR))) {
> move("$reportsdir/$file", "$oldreportsdir/$file")
- 'readdir' will give you directories as well as plain files. I suggest
my @files = grep -f, readdir(DIR);
foreach (@files) {
:
}
- What's this 'move' thing? Have you sneakily added 'use File::Copy without
telling us? If so, then
move("$reportsdir/$file", $oldreportsdir);
is clearer.
- Do you want existing files of the same name overwritten? If not you need
to check first whether the destination file exists.
- Try your program first with 'copy' instead of 'move'. Then if it doesn't work
you shouldn't have done any damage.
> or die "move failed: $!";
> }
>
Hmm. That's probably not the answer you were hoping for!
Even so HTH,
Rob
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]