ExtMail服务器社区's Archiver

fengyong 发表于 2008-4-7 09:44

递归ftp文件目录

星期天在学习一个ftp 同步脚本写的。

[code]
#!/usr/bin/perl
#
#
use strict;
use Net::FTP;

my $ftp=Net::FTP->new('localhost',debug=>1);
$ftp->login("username","passwd") or die $ftp->message;

#my @dir=$ftp->dir();

#foreach (@dir){
#               next if /\.$/ or /\.\.$/;
#               print "$_ \n";
#}
#
scan_ftp($ftp,'/');
sub scan_ftp {
        my $ftp=shift;
        my $path=shift||'/';
        print "PATH: $path\n";
        my $dir=$ftp->dir($path);

        for my $f (@$dir){
        next if $f=~m/^d.+\s\.\.?$/;
        #print "F $f\n";
        my $n=(split(/\s+/,$f,9))[8];
        my $type=substr($f,0,1);
        #print "TYPE: $type\n";
        my $name=$path.$n;
        if ($type eq 'd'){
        $name.="/";
        scan_ftp($ftp,$name);
        }else{
            print "FILE: $name \n";
        }
    }
}
[/code]

hzqbbc 发表于 2008-7-6 08:51

顶yong哥,欢迎多发这样有价值的帖子,方便其他人学习呢。~:hug:

hzqbbc 发表于 2008-7-6 08:53

看了下,核心就是scan_ftp这个函数,递归的扫描~ 强顶~

页: [1]

Powered by Discuz! Archiver 7.0.0  © 2001-2009 Comsenz Inc.