Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Perl (http://www.go4expert.com/articles/perl-tutorials/)
-   -   Recursive Directory Listing of remote server throught FTP (http://www.go4expert.com/articles/recursive-directory-listing-remote-t2348/)

pradeep 26Dec2006 18:32

Recursive Directory Listing of remote server throught FTP
 
Getting a recursive listing of all the specified directories within a specified path using FTP. We sometimes requires to calculate the number of directories or the number of files, and sometimes even the total size of files inside the specified directory.
The following code is written to get the recursive directory listing, it can me modified for file listing and/or total filesize.

Code: Perl

# required modules
 use Net::FTP;
 use File::Listing qw(parse_dir);
 
 sub getRecursiveDirListing
  {
      # create a new instance of the FTP connection
      my $ftp = Net::FTP->new("localhost", Debug=>0) or die("Cannot connect $!");
 
      # login to the server
      $ftp->login("go4e","g4e") or die("Login failed $!");
 
      # create an array to hold directories, it should be a local variable
      local @dirs = ();
     
      # directory parameter passed to the sub-routine
      my $dir = $_[0];
 
      # if the directory was passed onto the sub-routin, change the remote directory
      $ftp->cwd($dir) if($dir);
 
      # get the file listing
      @ls = $ftp->ls('-lR');
 
      # the current working directory on the remote server
      my $cur_dir = $ftp->pwd();
 
      # parse and loop through the directory listing
      foreach my $file (parse_dir(\@ls))
      {
          my($name, $type, $size, $mtime, $mode) = @$file;
 
          next if($type ne 'd'); # ignore current entry if not a directory and continue
         
          push(@dirs,"$cur_dir/$name"); # push the directory into the array
 
          # recursive call to get the entries in the entry, and get an array of return values
          @xx = getRecursiveDirListing ("$cur_dir/$name");
      }
 
      # close the FTP connection
      $ftp->quit();
 
      # merge the array returned from the recursive call with the current directory listing
      return (@dirs,@xx);
  }



Usage

Code: Perl

@y = getRecursiveDirListing ("/var/www/html");
  # @y contains all directories inside /var/www/html
 
  print join(";",@y);


Rakish 26Dec2006 21:38

Re: Recursive Directory Listing of remote server throught FTP
 
Pradeep,

Sounds like a great solution,

Thank You,

Rakish

pradeep 26Dec2006 21:40

Re: Recursive Directory Listing of remote server throught FTP
 
Well, I did it because of my need, I remembered your earlier query about Directory Size. So, try to modified this code for getting directoru size!

dijas78 27Aug2009 03:09

Re: Recursive Directory Listing of remote server throught FTP
 
Quote:

Originally Posted by pradeep (Post 7045)
Well, I did it because of my need, I remembered your earlier query about Directory Size. So, try to modified this code for getting directoru size!

dear i create my host FTP can i do anyting there tell me

do this than tell me how


i want my own broadcast server on udp and tcp can tell me how do this

pradeep 27Aug2009 10:21

Re: Recursive Directory Listing of remote server throught FTP
 
Quote:

Originally Posted by dijas78 (Post 56238)
dear i create my host FTP can i do anyting there tell me

do this than tell me how


i want my own broadcast server on udp and tcp can tell me how do this

I didn't get what exactly you would like to do!

dijas78 29Aug2009 20:15

Re: Recursive Directory Listing of remote server throught FTP
 
Quote:

Originally Posted by pradeep (Post 56266)
I didn't get what exactly you would like to do!

u know hindi my english very weeek
than i tell u what want me

caesurus 9Sep2009 20:27

Re: Recursive Directory Listing of remote server throught FTP
 
Hi...

Just a comment on your code. It is very inefficient to open a new FTP connection every time you call getRecursiveDirListing(), depending on the server it can add several seconds of delay per directory listing. It is A LOT faster if you open the FTP socket before calling getRecursiveDirListing() and leave it open the whole time. If you try this code on a remote server instead of your localhost server you'd see the increase in speed.

EG:
Code:

use Net::FTP;
 use File::Listing qw(parse_dir);

 sub getRecursiveDirListing
 {
      # create an array to hold directories, it should be a local variable
      local @dirs = ();

      # directory parameter passed to the sub-routine
      my $dir = $_[0];

      # if the directory was passed onto the sub-routin, change the remote directory
      $ftp->cwd($dir) if($dir);

      # get the file listing
      @ls = $ftp->ls('-lR');

      # the current working directory on the remote server
      my $cur_dir = $ftp->pwd();

      # parse and loop through the directory listing
      foreach my $file (parse_dir(\@ls))
      {
          my($name, $type, $size, $mtime, $mode) = @$file;

          next if($type ne 'd'); # ignore current entry if not a directory and continue

          push(@dirs,"$cur_dir/$name"); # push the directory into the array

          # recursive call to get the entries in the entry, and get an array of return values
          @xx = getRecursiveDirListing ("$cur_dir/$name");
      }

      # merge the array returned from the recursive call with the current directory listing
      return (@dirs,@xx);
  }

Usage being:
Code:

print("Connecting to FTP ... ");
# create a new instance of the FTP connection
$ftp = Net::FTP->new("ftp.hp.com", Debug=>0) or die("Cannot connect $!");

# login to the server
$ftp->login("anonymous","anonymous") or die("Login failed $!");
print(" Logged in ... \n");
@y = getRecursiveDirListing ("/pub/softlib/");
print join("\n",@y);

# close the FTP connection
$ftp->quit();

Thanks for the code, it helped me to get started. I usually don't program in perl :)

Thanks again.


All times are GMT +5.5. The time now is 19:56.