| need help to change the perl script
|
cccc

msg:444717 | 12:11 am on Mar 4, 2004 (gmt 0) | hi this perl script opens the info file, reads the name of the first file, renames the first file, sends via ftp, waits 15 minutes, rename the second file and sends via ftp to the same server. the problem is, it sends files to the same directory. how to change it, to send the second file to the different directory? for example to: FTP/IN/SECOND? #!/usr/bin/perl use strict; use warnings; use Net::FTP; # change directory chdir "/ftp/files" or die "/ftp/files: $!\n"; # DO NOT transfer without info file -f "/ftp/files/info" or die "info file is missing\n"; open(FILE, "<info>"); while (<FILE> ) { s/\W*$//; next if (!$_); /^(.+?) \s+ (.+?)$/x; my ($old, $new) = ($1, $2); rename $old, $new; # ftp transfer my $server = "X.X.X.X"; my $ftp = Net::FTP->new ($server, Timeout => 9000, Debug => 3); $ftp or die "$server: cannot connect: $@"; # If you don't use ~/.netrc $ftp->login ('anonymous', '****@domain.net') or die "$_: cannot logon: " . $ftp->message; # change remote directory for the first file $ftp->cwd("FTP/IN/FIRST"); # Put first file to the ftp server $ftp->put ($2) or die "$server: cannot put $2: " . $ftp->message; sleep (15 * 60) }
|
cccc

msg:444718 | 12:34 pm on Mar 6, 2004 (gmt 0) | hi I've tried this: --------------------------------------------------------------
# change remote directories my $cwd_performed = 0; if ($cwd_performed => 0) { $ftp->cwd("FTP/IN/FIRST") } else { $ftp->cwd("FTP/IN/SECOND") if (!$cwd_performed++) } -------------------------------------------------------------- but it still sends both files to the same directory. any idea, what's wrong?
|
Damian

msg:444719 | 12:59 pm on Mar 6, 2004 (gmt 0) | hi cccc, Do you set $cwd_performed = 1 or do $cwd_performed++ somewhere? I didn't see it in your script. If $cwd_performed is always 0 you would always ftp to the same directory (the second one in your latest post) I'm just an amateur programmer but I have never seen this notation: if (!$cwd_performed++) $cwd_performed++ means "Add 1 to $cwd_performed" I wonder what if (!$cwd_performed++) is supposed to do? I read it as "if 1 can not be added to $cwd_performed" which seems strange. Maybe it should read if (!$cwd_performed) meaning "if $cwd_performed does not have a value"
|
cccc

msg:444720 | 1:47 pm on Mar 6, 2004 (gmt 0) | hi Damian thanks for HELP the whole script looks: #!/usr/bin/perl -w use strict; use warnings; use Net::FTP; # change directory chdir "/ftp/files" or die "/ftp/files: $!\n"; # DO NOT transfer without info file -f "/ftp/files/info" or die "info file is missing\n"; open(FILE, "<info>"); while (<FILE> ) { s/\W*$//; next if (!$_); /^(.+?) \s+ (.+?)$/x; my ($old, $new) = ($1, $2); rename $old, $new; # ftp transfer my $server = "X.X.X.X"; my $ftp = Net::FTP->new ($server, Timeout => 9000, Debug => 3); $ftp or die "$server: cannot connect: $@"; # If you don't use ~/.netrc $ftp->login ('anonymous', '****@domain.net') or die "$_: cannot logon: " . $ftp->message; # change remote directories my $cwd_performed = 0; if ($cwd_performed) { $ftp->cwd("FTP/IN/FIRST") } else { $ftp->cwd("FTP/IN/SECOND") if (!$cwd_performed++) } # Put first file to the ftp server $ftp->put ($2) or die "$server: cannot put $2: " . $ftp->message; $ftp->quit; sleep (15 * 60) } the first file should be sent to the fixed remote directory: "FTP/IN/FIRST" and the second file to: "FTP/IN/SECOND" but it sends both files to the same directory.
|
Damian

msg:444721 | 2:13 pm on Mar 6, 2004 (gmt 0) | I think this should work (not tested): Substitute [perl] # change remote directories my $cwd_performed = 0; if ($cwd_performed) { $ftp->cwd("FTP/IN/FIRST") } else { $ftp->cwd("FTP/IN/SECOND") if (!$cwd_performed++) } by # change remote directories my $cwd_performed; if ($cwd_performed ne "1") { # if $cwd_performed is not 1 $ftp->cwd("FTP/IN/FIRST"); # ftp to first $cwd_performed = "1"; # set $cwd_performed to 1 } else { # $cwd_performed is 1 $ftp->cwd("FTP/IN/SECOND"); # ftp to the second undef $cwd_performed; #undef $cwd_performed just to be sure } [/perl]
|
cccc

msg:444722 | 2:32 pm on Mar 6, 2004 (gmt 0) | I've changed, but still sends both files to the same directory: Net::FTP=GLOB(0x838c82c)<<< 220 szhstorage Microsoft FTP Service (Version 5.0). Net::FTP=GLOB(0x838c82c)>>> user anonymous Net::FTP=GLOB(0x838c82c)<<< 331 Anonymous access allowed, send identity (e-mail name) as password. Net::FTP=GLOB(0x838c82c)>>> PASS .... Net::FTP=GLOB(0x838c82c)<<< 230 Anonymous user logged in. [Sat Mar 6 15:25:09 2004] ftp5.cgi: Use of uninitialized value in string ne at ftp5.cgi line 47, <FILE> line 1. Net::FTP=GLOB(0x838c82c)>>> CWD FTP/IN/FIRST Net::FTP=GLOB(0x838c82c)<<< 250 CWD command successful. Net::FTP=GLOB(0x838c82c)>>> PORT 192,168,0,1,142,8 Net::FTP=GLOB(0x838c82c)<<< 200 PORT command successful. Net::FTP=GLOB(0x838c82c)>>> STOR AA3200204 Net::FTP=GLOB(0x838c82c)<<< 150 Opening ASCII mode data connection for AA3200204. Net::FTP=GLOB(0x838c82c)<<< 226 Transfer complete. Net::FTP=GLOB(0x838c82c)>>> QUIT Net::FTP=GLOB(0x838c82c)<<< 221 Net::FTP=GLOB(0x83dfc64)<<< 220 szhstorage Microsoft FTP Service (Version 5.0). Net::FTP=GLOB(0x83dfc64)>>> user anonymous Net::FTP=GLOB(0x83dfc64)<<< 331 Anonymous access allowed, send identity (e-mail name) as password. Net::FTP=GLOB(0x83dfc64)>>> PASS .... Net::FTP=GLOB(0x83dfc64)<<< 230 Anonymous user logged in. [Sat Mar 6 15:25:19 2004] ftp5.cgi: Use of uninitialized value in string ne at ftp5.cgi line 47, <FILE> line 2. Net::FTP=GLOB(0x83dfc64)>>> CWD FTP/IN/FIRST Net::FTP=GLOB(0x83dfc64)<<< 250 CWD command successful. Net::FTP=GLOB(0x83dfc64)>>> PORT 192,168,0,1,142,10 Net::FTP=GLOB(0x83dfc64)<<< 200 PORT command successful. Net::FTP=GLOB(0x83dfc64)>>> STOR BB20020417 Net::FTP=GLOB(0x83dfc64)<<< 150 Opening ASCII mode data connection for BB20020417. Net::FTP=GLOB(0x83dfc64)<<< 226 Transfer complete. Net::FTP=GLOB(0x83dfc64)>>> QUIT Net::FTP=GLOB(0x83dfc64)<<< 221
|
cccc

msg:444723 | 4:02 pm on Mar 7, 2004 (gmt 0) | I found out this works well:
this one works:my $cwd_performed = 0; while (<FILE>) {................. ......................................... ......................................... # change the directory if ($cwd_performed > 0) { $ftp->cwd("/FTP/IN/SECOND") } else { $ftp->cwd("/FTP/IN/FIRST") if (!$cwd_performed++) } thanks anyway! now I have other problem. how to change this script ,to check if file names are the same like on the info file? otherwise should stop the whole process and not transfer any files. should have 2 date files and one info file each time. info file looks: ------------------------- VA12300017 ZE20040212 WA12300018 ZN20040212 ------------------------- this perl script first opens and reads the info file, renames the first file VA12300017 to ZE20040212, sends via ftp the first file, waits 20 minutes, renames the second file WA12300018 to ZN20040212 and sends via ftp the second file.
|
|
|