Operating System - HP-UX
1825010 Members
3731 Online
109678 Solutions
New Discussion юеВ

need a perl script to read from text file and rename files

 
SOLVED
Go to solution
'chris'
Super Advisor

Re: need a perl script to read from text file and rename files

hi

thanks, it works very well.

sorry for asking again, but I have
still only one problem with:

-f $1 or next;
-f $2 and next;

in most cases the second file will be transfered first and I don't know how to change it.

if I try with

-f $1 or next;
-f $2 or next;

then wont work and I get in the log:
"files are not completed."

for me are very importannt 2 things:
- the second file cannot be transfered
before the first one
- first and second cannot be transfered twice

the code now is:

@ARGV = ("info.txt");
my %ren;
while (<>) { # for every line in info.txt
m/^\s*(\S+)\s+(\S+)\s*$/ or next; # That matches like "XXXXXXXX XXXXXXXX"
-f $1 or next; # skip if file to rename does not exist
-f $2 and next; # skip if file to rename does not exist
$ren{$1} = $2;
}

unless (2 == scalar keys %ren) {
die "files are NOT complete !\n";
# extend to your feeling
}

foreach my $f1 (keys %ren) {
my $f2 = $ren{$f1};
move ($f1, $f2); # Rename the file

my $server = "192.168.0.1";
my $user = "anonymous";
my $passwd = "";
my $ftp = Net::FTP->new ($server, Timeout => 9000, Debug => 3); # Default timeout od 120 (2minutes) is too short
$ftp or die "$server: cannot connect: $@";
# If you don't use ~/.netrc
$ftp->login ($user,$passwd) or
die "$_: Could not login: " . $ftp->message;

# Put file 2 (not 1) to the ftp server
# VPN connection might go down, so retry twice

foreach my $try (0 .. 2) {
$ftp->put ($f2) and last; # Success
if ($try == 2) {

# Put file 2 (not 1) to the ftp server
# VPN might go down, so retry twice
foreach my $try (0 .. 2) {
$ftp->put ($f2) and last; # Success
if ($try == 2) {
open my $mail, "| /usr/sbin/sendmail -t" or die "Cannot send mail: $!\n";
print $mail "To: $mail\n",
"From: $linux\n",
"Subject: ftp transfer was NOT successfull\n",
"\n------------------------------------------------------------------\n",
"ftp transfer was NOT successfully\n\n";
"Time: ". scalar localtime ."\n",
"------------------------------------------------------------------\n.\n";
close $mail;
die "$server: cannot put $f2: " . $ftp->message;
}
print STDERR "Trasfer of $f2 failed: ",$ftp->message, ", retry in 3 minutes\n";
sleep (3 * 60);
}
$ftp->quit;

# And wait 5 minutes
sleep (5 * 60);
}

I tried to fix self, but I can't.
I know, I asked to many times ,but you are only one person, who can help me.

kind regards
chris
H.Merijn Brand (procura
Honored Contributor

Re: need a perl script to read from text file and rename files

This is because the order the keys of a hash are returned is undefined by design

Here is what to change to preserve the order:

--8<---
@ARGV = ("info.txt");
my @ren;
while (<>) { # for every line in info.txt
m/^\s*(\S+)\s+(\S+)\s*$/ or next; # That matches like "XXXXXXXX XXXXXXXX"
-f $1 or next; # skip if file to rename does not exist
-f $2 and next; # skip if file to rename does not exist
push @ren, [ $1, $2 ];
}

unless (@ren == 2) {
die "files are NOT complete !\n";
# extend to your feeling
}

foreach my $f (@ren) {
my ($f1, $f2) = @$f;
move ($f1, $f2); # Rename the file
-->8---

Of course there are many more ways to do this, but this is by far the easiest

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
'chris'
Super Advisor

Re: need a perl script to read from text file and rename files

thank you very much again for your time
and help

sorry to disturb again, but the script
stop now at:

foreach my $f2 (values %ren) {

move $f2, "/home/test/out/$f2";
move "info.txt", "/home/test/out/info.txt";
move "/home/test/logs/errorlog.txt", "/home/test/out/errorlog.txt";

with the message:
Global symbol "%ren" requires explicit package name at ftp3.cgi line 115.
Execution of ftp3.cgi aborted due to compilation errors.
Errors:

I tried to change to:

foreach my $f2 (values @ren) {

but it doesn't help:
"Type of arg 1 to values must be hash (not private array)"

greetings
chris
H.Merijn Brand (procura
Honored Contributor

Re: need a perl script to read from text file and rename files

That's because there is no more %ren :)

--8<---
foreach my $f2 (map { $_->[1] } @ren) {

move $f2, "/home/test/out/$f2";
-->8---

Time to learn Perl? Buy a book?

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
'chris'
Super Advisor

Re: need a perl script to read from text file and rename files

you are right!
I will do

thanks
chris
'chris'
Super Advisor

Re: need a perl script to read from text file and rename files

hi procura

sorry to ask again, but I have a problem and I don't know how to solve it.

I try to send a mail with log file contents
and I've put on the end of the script following code:

................................................
................................................
................................................

die "$server: cannot put $ft2: " . $ftp->message;
}
print STDERR "Trasfer of $ft2 failed: ",$ftp->message, ", retry in 15 minutes\n";
sleep (15 * 60);
}
$ftp->quit;

# wait 5 minutes before processing next file.
sleep (5 * 60);
}

# create backup subfolder when transfer completed
my @dt = localtime;
my $subfolder_name = ((((1900 + $dt[5]) * 100 + 1 + $dt[4]) * 100 + $dt[3]) * 100 + $dt[2]) * 100 + $dt[1];

mkdir "/home/test/out/$subfolder_name" or die "$subfolder_name: $!";

# backup all files
foreach my $ft2 (map { $_->[1] } @ren) {
move $ft2, "/home/test/out/$subfolder_name/$ft2";
move "info", "/home/test/out/$subfolder_name/info";
move "/home/test/logs/errorlog.txt", "/home/test/out/$subfolder_name/errorlog.txt";
}

# read log file

my $content;
open(FILE, "/home/test/out/$subfolder_name/errorlog.txt") || die "Cant open file. Reason: $!"; # (-:
while() {
$content .=$_; # get all of the file into content including past new lines
}
close(FILE);

# send mail with log contents, when transfer completed

open(MAIL, "|/usr/sbin/sendmail -t") || die "Cant send mail. Reason: $!";
print MAIL "to:$mail\n";
print MAIL "from:$linux\n";
print MAIL "subject:transfer of 2 files was successfully !\n";
print MAIL "log file: \n";
print MAIL "$content \n\n";
print MAIL "Time: ", scalar localtime, "\n";
close(MAIL);
exit;

mail with the log contents will be sent, but the problem is:
after the second file was sent and all files are saved, it waits half hour and only after this time sends a confirmation mail.

how to change this script, to send a mail immediately after backup ?

greetings
chris
'chris'
Super Advisor

Re: need a perl script to read from text file and rename files

has someone any idea what's wrong ?
H.Merijn Brand (procura
Honored Contributor

Re: need a perl script to read from text file and rename files

Have you tried

close STDERR;

just before you are done will all successes? What happens is that you write the log to STDERR, and later open the log from the file that STDERR is writing to.

close STDERR;

will sync the output, so the log reader can continue.

This is however NOT a good solution, because STDERR is also used for die () and warn (). And you might need them on other failures.

The solution IS to explicitely open a stream to LOG, and write all errors/warnings to LOG instead of to STDERR, and close LOG before you open it for reading

Enjoy, Have FUN! H.Merijn [ who hopes that this analysis is correct ]
Enjoy, Have FUN! H.Merijn
'chris'
Super Advisor

Re: need a perl script to read from text file and rename files

hi procura

I don't know exactly what or how do you mean.

I tried to put

close STDERR;

before

# create backup subfolder when transfer completed
my @dt = localtime;

but the problem still exists.

greetings
chris