Unable to get the size of remote file using Net::FTP Perl Script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Unable to get the size of remote file using Net::FTP Perl Script
# 1  
Old 12-22-2011
Unable to get the size of remote file using Net::FTP Perl Script

Hi,

I am using below piece of code to get the size of the remote file.

Code:
            $ftp->cwd($destination) or $error=$ftp->message;
            if(!$error)
            {
                $ftp->put($file) or  $error=$ftp->message;
                print "FTP size = [", $ftp->size($file), "] \n";
                print "Local size = [", (-s $file), "] \n";
                &log($failure_logfile,$file, "Error in copying file to destination ".$error) if($error);
                &log($success_logfile,$file,"Success") if(!$error);

Output is:

Code:
FTP size = []
Local size = [1992]

Need your help on this.

Thanks,
Farooq
# 2  
Old 12-22-2011
Where does $ftp come from?

What is this '&log'?

Why not just post your code?
# 3  
Old 12-22-2011
Here is the entire code:

Code:
#! /usr/bin/perl
use Net::FTP;
my  $ipadd    = $ARGV[0];
my  $user     = $ARGV[1];
my  $password = $ARGV[2];
my  $destination = $ARGV[3];
my $file      = $ARGV[4];
my $success_logfile='Success';
my $failure_logfile='Failure';
my $error;
if(($ipadd) && ($user) && ($password) && ($destination) && ($file))
{
  my $ftp = Net::FTP->new($ipadd) or $error="Cannot connect to $ipadd: $@";
  if(!$error)
  {
     $ftp->login($user,$password) or $error=$ftp->message;
     if(!$error)
     {
         if(-e $file)
         {
            $ftp->binary();

            $ftp->cwd($destination) or $error=$ftp->message;
            if(!$error)
            {
                $ftp->put($file) or  $error=$ftp->message;
                print "FTP size = [", $ftp->size($file), "] \n";
                print "Local size = [", (-s $file), "] \n";
                &log($failure_logfile,$file, "Error in copying file to destination ".$error) if($error);
                &log($success_logfile,$file,"Success") if(!$error);
            }
         }
     }
     $ftp->quit;
  }
}

sub log
{
    my $log_file        = shift;
    my $filename        = shift;
    my $result = shift;
    $log_file="log/".$log_file;
    if (!(-e "$log_file.csv"))
    {
         open  (FL,">$log_file.csv")||die "Cannot open Log File.$! \n";
         print FL "Filename,Result\n";
         close (FL);
    }
    open (LOG,">>$log_file.csv") || die "Cannot open Log File.$! \n";
    print LOG "$filename,$result \n";
    close (LOG);
}

# 4  
Old 12-22-2011
That Net::FTP works at all is a minor miracle, FTP is extremely hard to script, with very different output between servers. There's no standard layout for ls in ftp for example, meaning some ftp servers may not show the size at all.

If it succeeded in uploading the file, the size isn't going to be different IMHO. (Unless you didn't put FTP into binary mode!) If something happened, the FTP connection would probably break completely.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Output after a perl script gives a file with size zero.

Hi, I have a unix shell script which generates a flat file after connecting to Teradata servers to fetch tables and views and also picks up modified unix scripts from the specified paths. Later on the script calls a perl script to assign a value based on the type of object in the flat file which... (2 Replies)
Discussion started by: yohasini
2 Replies

2. HP-UX

[Solved] Unable to rename file in ftp server .Net:FTP perl

Hello All, I am trying to connect to ftp server and get the files. Also i need to rename the file in other ftp dir. rename method is not allowing me to rename the file in other dir. When i tried copy command by using net::FTP:FILE then perl says it is not installed. Can some body help me to... (2 Replies)
Discussion started by: krsnadasa
2 Replies

3. Shell Programming and Scripting

Perl - Net::FTP issue

Wondering if anyone can help or advise on following issue. The below script should simply connect to a different server and retrieve certain files for me. use lib "/xxxxx/xxxxx/xxxxx/xxxx"; use Net::FTP; my $directory = "xxxxxxxx"; my $destinationDir = "xxxxxxxxx"; my $filePrefix =... (1 Reply)
Discussion started by: meevagh
1 Replies

4. Shell Programming and Scripting

Net::Ftp in perl

I am trying to execute a script in another server, i used Net::Ftp module How to execute unix command in another server by using Net::Ftp module.. #!/usr/bin.perl ### Perl script to $ftp->login($user_name,'password') or die "Cannot login ", $ftp->message;... (2 Replies)
Discussion started by: pritish.sas
2 Replies

5. Shell Programming and Scripting

Corrupted Download with Net::FTP (perl)

Hello, I am trying to download several files from a remote FTP server using Net::FTP from within a perl script. The files download alright, except they appear to be corrupted. I know this because once after downloading, I use bzcat to pipe the files to a split command, and bzcat complains saying... (3 Replies)
Discussion started by: amcrisan
3 Replies

6. Shell Programming and Scripting

FTP Perl Script File Size Mismatch.

Hello, I've written a Perl script that copies a set of files from one server to another. Prior to transferring a file the script gets the file size from the source system and compares this to the file size received in the target system. Except that the file sizes are slightly different. ... (1 Reply)
Discussion started by: mbb
1 Replies

7. Shell Programming and Scripting

Shell script to FTP if file size is 0KB

Hi all, I was a UNIX shell script that will check for the file size of each file and it should be re directed to a text file say filesize.txt And if there are more than 10 0KB files then FTP should not happend and whenever there are less than 10 0KB files it should FTP the files. Kindly help me... (1 Reply)
Discussion started by: Codesearcher
1 Replies

8. Shell Programming and Scripting

PERL: NET::FTP..>Debug Messages

HI All, NET::FTP->new($server, DEBUG=>1); I need to get all the Debug Messages in an array or a file.... Please suggest!! (0 Replies)
Discussion started by: angad.makkar
0 Replies

9. Shell Programming and Scripting

Perl FTP - check file size

The FTP perl module does not have any function which checks if the file downloaded is of size 0. Is there any way in perl to check while getting the files through FTP? Sometimes, there might be a problem with FTP and the downloaded file maybe of size 0. Hence, I would like to FTP that file... (1 Reply)
Discussion started by: rahulrathod
1 Replies

10. Shell Programming and Scripting

perl + Net::FTP::Recursive

Problem: It will not advance to the next user in the list. It always dies right after it sends the 2/2 files from the first users dir. $USERLIST="/export/home/mxdooley/perl_ftp/userlist"; $USER_DIR="/export/home/mxdooley/perl_ftp/homes";... (2 Replies)
Discussion started by: Optimus_P
2 Replies
Login or Register to Ask a Question