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);
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
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.