现在的位置: 首页 > 综合 > 正文

用Perl写的自动上传整个目录的程序

2013年11月20日 ⁄ 综合 ⁄ 共 835字 ⁄ 字号 评论关闭

三年以前(抄)写的东西吧,今天要用,所以找了出来。
放在这儿备用。

#!/usr/bin/perl -w

use Net::FTP;
use strict;

my $server='xxx.xxx.xxx.xxx';
my $user='foo';
my $pw='bar';
my $rdir='/to';

my $ftp = Net::FTP->new($server);
$ftp->login($user,$pw) || die "Login failed";

$ftp->mkdir($rdir,1);

$ftp->cwd($rdir)|| die print "$rdir doesn't seem to exist on $server./n";

my $remote_start_dir = $ftp->pwd();

my $local_start_dir = '/from';
# my $local_start_dir = $ENV{PWD};

handle_dir($local_start_dir, $remote_start_dir);

sub handle_dir()
{
 my $local=$_[0];
 my $remote=$_[1];
 opendir (DIR, $local) || die "huh? $!";
 $ftp->mkdir($remote,1) || die "can't make $remote on $server/n";
 my @subdirs;
 my @all_files = grep !/^/./.?$/, readdir DIR;
 foreach(@all_files)
 {
  if (-d $local . "/" . $_)
  {
   push @subdirs, $_;
  }
  else
  {
   $ftp->put($local . "/" . $_, $remote . "/" . $_)|| die "$!";
  }
 }

 foreach (@subdirs)
 {
  handle_dir($local . "/" .$_, $remote . "/" . $_);
 }
}

$ftp->quit;

抱歉!评论已关闭.