the first problem is this line:
unless(/^([\w.-]) \s+ ([\w.-])$/x) {
and script doesn't want to process, although file are correct.
Heh. Yup, that one's my fault. Should be:
unless(/^([\w.-]+) \s+ ([\w.-]+)$/x) {
notice the extra pluses.
By the way, you can make your code a whole lot nicer to read by using <code> tags. For example:
<code>
unless(/^([\w.-]) \s+ ([\w.-])$/x) {
</code > (without this extra space)
Code tags mean that you don't have to put <br> tags after ever line. It makes it a tonne easier to read too.
it should read the filenames in, compare them to what is in the info file and if they do not match don't process.
for example: should NOT process with above info file and these files: AB0000015 / BB0000016
In order to do this you'll need to see if the files in the info file exist. Try something like this:
unless(/^([\w.-]+) \s+ ([\w.-]+)$/x) {
print STDERR "$_ is not a valid line";
next;
}
my ($old, $new) = ($1, $2);
unless( -e $old ) {
print STDERR "$old does not exist!\n";
next;
}
rename $old, $new;
Note that this won't stop you from processing other files in the info file. If you want to test that first each and every file mentioned in the info file exists and then transfer it you're going to have to completely change your code structure.
This new code structure would probably have you read in each line in from the file, check that everything's good and push the filenames onto an array. Then, when you know that all the files exist and match what you expect, you'd rename them and transfer them. Of course you could get a similar result by changing those "next"s to "die"s as well.
the second problem is, that after transfer doesn't move these 2 files to the backup directory
this line has an error:
system("mv $new /var/save/$subfolder_name");
Oh dear. You're using system. Make sure that you change your shebang line up the top of your script to look like this:
#!/usr/bin/perl -wT
notice the capital T. If you don't know about taint checking do a few searches for it. It's very straight forward and should save you lots of grief later.
So, what's wrong with your line there? Easy. $new doesn't exist outside of your for loop. So, you can either move this line up into your for loop, or your can come up with a different way to remember $new.
It might be a good idea to create your backup directory before you start processing your files, then move each successfully transferred file into that directory as you go. Then your final act will be to send an email and copy the info file in.
You might be pleased to know that there's a Perl module that makes moving files a little tidier though:
#!/usr/bin/perl -wT
use strict;
use warnings;
use File::Copy;
...
unless(move("$new", "/var/save/$subfolder_name")) {
print STDERR "Oops! Couldn't move the file: $!";
}
Note that File::Copy still uses system underneath so taint checking is still required.
my third problem is, it sends the mail or moved the files without ftp transfer
Yes, indeed it does.
By the time you get to the sending email/moving the files part of your script there is no indication of whether you transferred your files or not. You do know that at this point (since the script is still running) that nothing disasterous happened, but you don't know if you skipped cases you didn't like (by using next) or if you transferred your files, or if 1 file was transfered and the other wasn't.
Since I don't know how important it is to you that you successfully transfer both files etc, I can't tell you the exact way to fix things here, but one way to tell if you've transfered the file is to move it after successful transfer (as I suggested above) and then check to see if there's anything in the backup directory at this point.
Truth to tell, though, I think you probably want to rewrite the logic behind your script altogether to be more like this:
Create backup directory
Open file
for each line in info file
next if blank
get filename and rename from file
push filename and rename onto arrays
end for
if size of arrays is too small (smaller than @ftp_locations)
complain that I don't have enough files
exit;
end if
for each item in file array
rename file
ftp file
move file to backup directory
end for
send message saying everything worked.
Good luck,
jarich |