Recursive Directory Listing of remote server throught FTP

Discussion in 'Perl' started by pradeep, Dec 26, 2006.

  1. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    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:
     # 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:
      @y = getRecursiveDirListing ("/var/www/html");
      # @y contains all directories inside /var/www/html
      
      print join(";",@y);
      
     
  2. Rakish

    Rakish New Member

    Joined:
    Jun 30, 2006
    Messages:
    25
    Likes Received:
    0
    Trophy Points:
    0
    Pradeep,

    Sounds like a great solution,

    Thank You,

    Rakish
     
  3. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    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!
     
  4. dijas78

    dijas78 New Member

    Joined:
    Aug 26, 2009
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    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
     
  5. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    I didn't get what exactly you would like to do!
     
  6. dijas78

    dijas78 New Member

    Joined:
    Aug 26, 2009
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    u know hindi my english very weeek
    than i tell u what want me
     
  7. caesurus

    caesurus New Member

    Joined:
    Sep 9, 2009
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    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.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice