天天看點

[Perl]FTP自動上傳檔案的腳本以及配置檔案

本應用程式設計的幾個基本理念是:

工具箱再利用: 盡可能利用已有的工具;

簡化運作步驟; 不引入過多的業務邏輯,滿足的需求越簡單越好。

是以,我們定義了本應用程式依賴于以下幾個工具的運作:

ActivePerl-5.8.4.810-MSWin32-x86

Upload.pl

Upload.config

我們将主要的執行邏輯都放在Perl源檔案Upload.pl中了,配置檔案為Upload.config。

這個perl檔案将執行的任務是, 按照指定的檔案夾目錄,自動将該檔案夾下的所有檔案上傳到指定ftp站點的指定目錄下。

這個Perl腳本實際是從Uwe Keim 的《Perl Script for uploading modified files to a FTP-Server》繼承下來的,

隻不過增加了容錯反應和讀取外部配置檔案的部分,刨掉了與一般業務邏輯無關的讀寫access檔案的部分。

程式大緻的流程是:

第一步,嘗試登陸ftp站點;

第二步,在指定檔案夾A類下尋找符合條件的檔案,并将A類檔案上傳到FTP站點指定目錄下;

第三步,如果A類檔案們全部上傳成功,那麼在指定檔案夾B類下尋找指定檔案, 并且上傳到FTP指定目錄下

第四步,寫成功/失敗日志。

最後,我們要寫的成功/失敗日志的格式如下所示:

成功: 生成一個名為“Upload_Succ_2005_01_04_17_23.log”的日志檔案

檔案格式:輸出上傳時間,以及所有上傳檔案名及其大小和耗費的時間。

失敗: 生成一個名為“Upload_Fail_2005_01_04_17_23.log”的日志檔案

檔案格式:輸出上傳時間,以及已經上傳的檔案名及其大小和耗費的時間,和失敗的檔案名及原因。

配置perl腳本運作有兩個辦法:

您可以在Windows計劃任務中配置運作“Perl Upload.pl”的時間,這需要在Windows環境中配置ActivePerl 5.8.4.810;

您也可以利用Perl2Exe(p2x-8.40-Win32)來将perl腳本編譯為一個exe可執行程式,在計劃任務中運作這個exe(這需要PerlCRT.dll在系統路徑下)。

[注意!]在運作之前,您必須修改“Upload.config”檔案以配置所需的重要參數。

外部配置參數

在和perl腳本同一目錄下的“Upload.config”配置檔案中,是事先配置的六個外部參數:

參數1: ftp_server:

FTP伺服器的IP位址。

參數2: ftp_dir:

指定的FTP上傳目錄路徑;

參數3: ftp_uid:

FTP的登陸使用者名;

參數4: ftp_pw:

FTP的登陸密碼;

參數5: src_dir_WAVFiles,這是一個數組:

指定A類檔案夾,放置所有要上傳的語音檔案;

注意:這個目錄下的子檔案夾也會被上傳。

參數6: src_dir_NamesListFile,這是一個數組:

指定B類檔案夾,放置B類檔案.

注意:這個目錄下的子檔案夾也會被上傳。

附錄:

Upoad.pl内容:

#!/usr/bin/perl -w

####################################################################

#

# 工程項目: FTP自動上傳兩類檔案

#

# 子產品名稱: FTPAutoUpload

#

# 子產品任務: 按照指定的檔案夾目錄,自動将該檔案夾下的所有檔案上傳到指定ftp站點的指定目錄下

#

# 程式名稱: Upload.pl

#

# 程式員: Uwe Keim

# 鄭昀

# 曆史記錄:

# 編号 日期 作者 備注

# 1 2000 Uwe Keim 建立

# 2 2005.1.5 鄭昀 加入容錯處理和讀取外部配置檔案部分

#

####################################################################

##="===============================================================## ## 引用的庫聲明 2

#use strict;

use File::Copy;

use File::stat;

use File::Find;

use Net::FTP;

use Date::Pcalc qw(Delta_DHMS);

use Date::Parse;

use Win32::OLE;

use Win32::OLE::Variant;

##="===============================================================##

##================================================================##

## 引用的庫聲明 1

#- 讀取ini配置檔案的庫

use Config::IniFiles;

my $cfg = Config::IniFiles->new( -file =">" "Upload.config" );

##="===============================================================##

##================================================================##

## 從配置檔案讀取外部參數 ##

##

## FTP伺服器的IP位址 ##

$ftp_server = $cfg->val('FTPServer', 'ftp_server') || '';

## 指定的FTP上傳目錄路徑 ##

#! 切記:檔案夾最後不要加"/"符号 !#

$ftp_dir = $cfg->val('FTPServer', 'ftp_dir') || '';

## FTP的登陸使用者名 ##

$ftp_uid = $cfg->val('FTPServer', 'ftp_uid') || '';

## FTP的登陸密碼 ##

$ftp_pw = $cfg->val('FTPServer', 'ftp_pw') || '';

## 指定檔案夾“語音檔案”,放置所有要上傳的語音檔案 ##

#! 切記:檔案夾最後不要加"//"符号 !#

@src_dir_WAVFiles = $cfg->val('SrcDirectory', 'src_dir_WAVFiles');

## 指定檔案夾“命名對照清單檔案TXT”,放置命名對照清單檔案 ##

#! 切記:檔案夾最後不要加"//"符号 !#

@src_dir_NamesListFile = $cfg->val('SrcDirectory', 'src_dir_NamesListFile');

## 一個字元串集合,表明哪些類型的檔案/檔案夾将不被上傳到伺服器 ##

@wc_exclude = ("_vti",".mdb","//bak","//data","server.inc ");

##================================================================##

##================================================================##

## 記錄全部過程的日志檔案準備

$logfilename = 'upload.log';

$log_cnt = 0;

LOG("");

LOG("自動上傳FTP檔案 Version 0.1");

LOG("");

LOG("用法: Perl Upload.pl");

LOG("");

##="===============================================================##

##================================================================##

##=== 程式執行的第一步:嘗試登陸ftp站點 ="=========================## ## $total_files是上傳檔案的數目

$total_files = 0;

## $processed_files是已上傳檔案的數目

$processed_files = 0;

## $skipped_files是跳過檔案的數目

$skipped_files = 0;

## $start_date計算出目前開始的時間

$start_date = timeString(time);

## $g_nUploadSuccess代表是否已經完全上傳,-1為不是,1為是:

my $g_nUploadSuccess = 1;

## $g_nIsAllWAVsFile_UploadSuccess代表是否已經完全将A類檔案上傳,-1為不是,1為是:

my $g_nIsAllWAVsFile_UploadSuccess = 1;

## $g_strLastError代表上次錯誤原因:

my $g_strLastError = "";

LOG("正在連結至指定FTP伺服器($ftp_server)...");

$ftp = Net::FTP->new($ftp_server);

if($@)

{

$g_strLastError = "不能連接配接到FTP伺服器,錯誤原因:".$@;

LOG("$g_strLastError@/n ");

$g_nUploadSuccess = -1;

}

else

{

$ftp->login($ftp_uid, $ftp_pw);

if($@)

{

$g_strLastError = "不能登陸FTP伺服器,錯誤原因:".$@;

LOG("$g_strLastError/n");

$g_nUploadSuccess = -1;

}

else

{

$ftp->binary;

LOG("連結FTP伺服器成功!");

##="===============================================================##

##================================================================##

##=== 程式執行的第二步,将指定A類檔案夾下所有A類檔案上傳到FTP站點指定目錄下 ="==##

my %lookup;

LOG("準備上傳“A類檔案”目錄(@src_dir_WAVFiles)下的所有檔案!");

find(/&processFiles, @src_dir_WAVFiles);

LOG("目錄(@src_dir_WAVFiles)已經處理完畢,結果是:");

##="===============================================================## ##=== 程式執行的第三步,将指定B類檔案夾下B類檔案上傳到FTP站點指定目錄下 ="==## if($g_nIsAllWAVsFile_UploadSuccess > 0)

{

LOG("+="==============================+"");

LOG("準備上傳B類目錄(@src_dir_NamesListFile)下的所有檔案!");

find(/&processFiles, @src_dir_NamesListFile);

LOG("目錄(@src_dir_NamesListFile)已經處理完畢,結果是:");

LOG("-="==============================-"");

}

else

{

LOG("-===============================-");

LOG("由于A類檔案目錄并沒有完全上傳,是以本B類檔案不上傳!");

LOG("-===============================-");

}

##================================================================##

##================================================================##

# 日志檔案的最後是一個統計報告

$span = calcDeltaSeconds($start_date,timeString(time));

LOG("上傳結果:成功。/n花費:$span 秒, 總共處理了 $total_files 個檔案, 其中 $processed_files 上傳成功, 跳過了 $skipped_files 個檔案。");

$ftp->quit() or warn "unable to quit: $@/n ";

}

closeLogfile();

}

##="===============================================================##

##================================================================##

##=== 程式執行的第四步,寫成功日志 ="==============================## if($g_nIsAllWAVsFile_UploadSuccess > 0 && $g_nUploadSuccess > 0)

{

$logfilename = 'Upload_Succ_'.shortTimeString(time).'.log';

$log_cnt = 0;

LOG("");

LOG("FTP自動上傳檔案 Version 0.1");

LOG("");

LOG("上傳結果:成功。/n花費:$span 秒, 總共處理了 $total_files 個檔案, 其中 $processed_files 上傳成功, 跳過了 $skipped_files 個檔案。");

LOG("");

closeLogfile();

}

##="===============================================================##

##================================================================##

##=== 程式執行的第四步,寫失敗日志 ="==============================## if($g_nIsAllWAVsFile_UploadSuccess < 0 || $g_nUploadSuccess < 0)

{

$logfilename = 'Upload_Fail_'.shortTimeString(time).'.log';

$log_cnt = 0;

LOG("");

LOG("FTP自動上傳檔案 Version 0.1");

LOG("");

LOG("上傳結果:失敗。失敗原因:$g_strLastError。/n花費:$span 秒, 總共處理了 $total_files 個檔案, 其中 $processed_files 上傳成功, 跳過了 $skipped_files 個檔案。");

LOG("");

closeLogfile();

}

##="===============================================================##

####################################################################

## 以下是子函數體的定義

##-------------------------------------------------------------##

##

## 函數名稱:processFiles

## 功能:

## 得到指定檔案夾下的所有檔案以及子檔案夾,然後依次處理它們。

##

## 程式員: Uwe Keim

##

## 曆史記錄:

## 編号 日期 作者 備注

## 1 2000 Uwe Keim

##

##-------------------------------------------------------------##

sub processFiles

{

my $srcdir = fsToBs($File::Find::dir);

my $srcpath = fsToBs($File::Find::name);

my $base = fsToBs($File::Find::topdir);

foreach my $exclude (@wc_exclude) {

if ( index($srcpath, $exclude)>-1 ) {

$File::Find::prune = 1 if -d $srcpath;

return;

}

}

# no DIRECT processing of directories.

if ( -d $srcpath ) {

return;

}

my $dstdir = $srcdir;

my $dstpath = $srcpath;

$dstdir ="~" s{/Q$base/E}{$ftp_dir}is;

$dstpath ="~" s{/Q$base/E}{$ftp_dir}is;

$dstdir = bsToFs($dstdir);

$dstpath = bsToFs($dstpath);

processFile($srcpath,$dstpath,$dstdir);

}

sub processFile

{

my ($src,$dst,$dstdir) = @_;

$total_files++;

LOG("正在處理檔案 $total_files /"$src/"...");

# --------------------

# check time.

my $need_upload = 0;

# create time.

my $t1 = $lookup{$src};

my $t2 = timeString(stat($src)->mtime);

if ( not defined $t1 ) {

$lookup{$src} = $t2;

$need_upload = 1;

} else {

my $delta_sec = calcDeltaSeconds($t1,$t2);

$need_upload = 1 if $delta_sec>5; # 5 seconds as tolerance.

}

# --------------------

if ( $need_upload>0 ) {

$processed_files++;

LOG("正在上傳檔案:從源 /"$src/" 到目标 /"$dst/"...");

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

$ftp->put($src, $dst) or $g_nIsAllWAVsFile_UploadSuccess="-1; if($g_nIsAllWAVsFile_UploadSuccess < 0)

{

LOG("不能上傳檔案:從源 /"$src/" 到目标 /"$dst/" (dst-dir: /"$dstdir/")。/n");

if($@)

{

LOG("錯誤原因是:$@/n");

}

}

} else {

$skipped_files++;

}

}

####################################################################

sub bsToFs {

my ($s) = @_;

$s ="~" s///gis;

return $s;

}

sub fsToBs {

my ($s) = @_;

$s ="~" s///gis;

return $s;

}

sub timeString {

my ($tm) = @_;

my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($tm);

return sprintf("%04d-%02d-%02d %02d:%02d:%02d", $year+1900, $mon+1, $mday, $hour, $min, $sec);

}

sub shortTimeString {

my ($tm) = @_;

my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($tm);

return sprintf("%04d_%02d_%02d_%02d_%02d", $year+1900, $mon+1, $mday, $hour, $min);

}

# input dates as string "YYYY-MM-DD HH:MM:SS".

# earlier as first parameter, later as second.

sub calcDeltaSeconds {

my ($t1,$t2) = @_;

my ($year1,$month1,$day1,$hh1,$mm1,$ss1) = scanDate($t1);

my ($year2,$month2,$day2,$hh2,$mm2,$ss2) = scanDate($t2);

my ($days, $hours, $minutes, $seconds) = Delta_DHMS(

$year1, $month1, $day1, $hh1, $mm1, $ss1, # earlier.

$year2, $month2, $day2, $hh2, $mm2, $ss2); # later.

return $seconds + $minutes*60 + $hours*60*60 + $days*60*60*24.

}

sub removeFilename {

my ($s) = @_;

my $pos = rindex($s,'//');

return substr($s, 0, $pos);

}

# format: "2000-09-29 09:09:51".

sub scanDate {

my ($date) = @_;

my ($year, $month, $day, $hour, $minute, $seconds);

$year = substr($date, 0, 4);

$month = substr($date, 5, 2);

$day = substr($date, 8, 2);

$hour = substr($date, 11, 2);

$minute = substr($date, 14, 2);

$seconds = substr($date, 17, 2);

return ($year, $month, $day, $hour, $minute, $seconds);

}

####################################################################

sub LOG {

my ($text) = @_;

my $time = timeString time;

# log to stdout.

print "[$time] $text/n";

# log to logfile.

my $LOG_STEP = 10;

flushLogfile() if ($log_cnt % $LOG_STEP)="=0" or $log_cnt="=0; $log_cnt++;

print HLOG "[$time] $text/n";

}

sub openLogfile {

closeLogfile();

open(HLOG,">>$logfilename") or die("打開日志檔案出錯:檔案名為 $logfilename ;錯誤原因為: $!");

};

sub closeLogfile {

close HLOG if defined HLOG;

}

sub flushLogfile {

closeLogfile();

openLogfile();

}

####################################################################

附錄:

Upoad.config内容:

##="===============================================================## ## 配置的外部參數 ##

##

[FTPServer]

#- FTP伺服器的IP位址 -#

ftp_server =" #- 指定的FTP上傳目錄路徑 -#

#! 切記:檔案夾最後不要加"/"符号 !#

ftp_dir =" #- FTP的登陸使用者名 -#

ftp_uid =" #- FTP的登陸密碼 -#

ftp_pw ="

##================================================================##

##================================================================##

## 配置的外部參數 ##

##

[SrcDirectory]

#- 指定檔案夾“語音檔案”,放置所有要上傳的語音檔案 -#

#! 切記:檔案夾最後不要加"/"符号 !#

src_dir_WAVFiles ="

#- 指定檔案夾“命名對照清單檔案TXT”,放置命名對照清單檔案 -#

#! 切記:檔案夾最後不要加"/"符号 !#

src_dir_NamesListFile =" ##================================================================##