免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1953 | 回复: 4
打印 上一主题 下一主题

谁能帮我看一下我刚写的这段遍历FTP文件的代码 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-03-28 14:45 |只看该作者 |倒序浏览
print "*********************************************************\n";
print " FTP Find Tool\n";
print " Build by YangKang \n";
print "*********************************************************\n";
$ARGC=@ARGV;
if ($ARGC!=1) {
print "\n Usage: $0 ftpListFile\n";
exit;
}

use Net::FTP;

#use Strict;
use Socket;
my $ftpfile=$ARGV[0];
use Net::FTP;

sub find_type
{
  my $path = shift;
  my $a=substr($path,0,1);
  return $a;
}


##########################################################
sub list
{
                my $current = $_[0];
                my @subdirs;
                $current=substr($current,1);
                print "$current \n";
                $ftp->cwd($current);
                my @dir=$ftp->dir($current);
                print "\n", "-" x 70, "\n";
                print "@dir\n";
                foreach(@dir)
                {
                  my $path=$ftp->pwd();
                  print "$_\n";
                  my $filetype=&find_type($_);
                  #print "$filetype\n";
                  $filename=substr($_,55);
                  #print "$filename\n";
                  if(&find_type($_) eq "d"
                  {
                    if($filename ne "."
                    {
                      if($filename ne ".."
                      {
                        $currentpath=$path . "/".$filename;
                        #print "$currentpath\n";
                        push @subdirs,$currentpath;

                      }
                    }
                  }
                  else
                  {
                    print $path."/$filename\n";
                  }
                }
                foreach (@subdirs)
                {
                    #print "$_\n";
                    &list($_);
                }
}

#read from file
open(USERFILE,$ftpfile)||die("Can't open the user's list!\n";
@data=<USERFILE>;
foreach(@data)
{
        my @a=split /\s+/,$_;
        $address=shift(@a);
        $port=shift(@a);
        $username=shift(@a);
        $password=shift(@a);
        print "Testing on $addressport usernameusername;passpassword ...\n";
        $ftp=Net::FTP->new($address,Port => $port);
        die "couldn't connect!\n" unless $ftp;
        $ftp->login($username,$password);
        if($ftp->cwd('/'))
        {
                print "FTP Serveraddress can be connected now!\n";
                &list('/');

        }
        else
        {
                print "FTP Serveraddress cann't be connected now!\n";
        }
        $ftp->quit();
}
close (USERFILE);
exit(0);

论坛徽章:
0
2 [报告]
发表于 2007-03-28 14:49 |只看该作者

回复 1楼 rubilly 的帖子

我想用递归的方法来遍历,运行需要一个文件,格式为:
ip port name passwd
可是总是无法遍历目录,出现如下的结果:
*********************************************************
FTP Find Tool
Build by YangKang
*********************************************************
Testing on luzg.ihep.ac.cn:21 username:lily;pass:lily727 ...
FTP Server:luzg.ihep.ac.cn can be connected now!


----------------------------------------------------------------------
dr-x------    2 501      501          4096 Mar 28 00:59 file drwx------    5 501      501          4096 Mar 27 13:18 upload
dr-x------    2 501      501          4096 Mar 28 00:59 file
drwx------    5 501      501          4096 Mar 27 13:18 upload
/ file

----------------------------------------------------------------------

/ upload

----------------------------------------------------------------------

为什么my @dir=$ftp->dir($current);这句起不了作用了呢?

论坛徽章:
0
3 [报告]
发表于 2007-03-28 22:39 |只看该作者
你把Debug => 1参数打开,看看问题出在什么地方
我刚写了一个Ftp递归找文件的程序,明天可以给你参考一下,不过不知道跟你的需求是不是一致

论坛徽章:
0
4 [报告]
发表于 2007-03-29 11:44 |只看该作者

回复 3楼 xiaoquqi 的帖子

好的 谢谢
我也是用递归做的

论坛徽章:
0
5 [报告]
发表于 2007-03-29 18:25 |只看该作者

  1. sub GetRemoteFile
  2. {
  3.     my $dir = shift;
  4.     my $file_hash;
  5.     #recursion to get true file
  6.     #my @file = $ftp->ls($dir);
  7.     my @file = GetFtpFile($dir);
  8.     my @file = @remote_file_list;
  9.     undef @remote_file_list;
  10.     foreach my $file (@file){
  11.         my $file_size = $ftp->size($file);
  12.         my $update_time = $ftp->mdtm($file);
  13.         #$update_time = Second_Date($update_time);
  14.         my ($path,$filename) = SplitFilePath($file);
  15.         $file_hash->{$filename}->{full_path} = $file;
  16.         $file_hash->{$filename}->{path} = $path;
  17.         $file_hash->{$filename}->{filename} = $filename;
  18.         $file_hash->{$filename}->{update_time} = $update_time;
  19.         $file_hash->{$filename}->{size} = $file_size;
  20.     }
  21.     return $file_hash;
  22. }
  23. sub GetFtpFile
  24. {
  25.     my $dir = shift;
  26.     my @list = $ftp->ls($dir);
  27.     foreach my $list (@list){
  28.         my $isDir = FtpisDir($list);
  29.         if($isDir == 1){
  30.             GetFtpFile($list);
  31.         }elsif($isDir == 2){
  32.             next;
  33.         }else{
  34.             push @remote_file_list,$ftp->ls($list);
  35.         }
  36.     }
  37. }
  38. sub FtpisDir
  39. {
  40.     #if a dir return 1,if the dir is empty return 2,else return 0
  41.     my $dir = shift;
  42.     my $cwd_retu = $ftp->cwd($dir);
  43.     my $size_retu = $ftp->size($dir);
  44.     if($cwd_retu == 1){
  45.         return 1;
  46.     }elsif(defined($size_retu)){
  47.         return 0;
  48.     }else{
  49.         return 2;
  50.     }
  51. }

复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP