The perl script and configuration file for FTP Automatic File Upload

Source: Internet
Author: User
Tags ftp file ftp login ftp site perl script

The basic concepts of this application design are as follows:
Toolbox reuse: use existing tools as much as possible;
Simplify the operation steps. Do not introduce too many business logic. The simpler the requirements, the better.

Therefore, we define that this application depends on the running of the following tools:
ActivePerl-5.8.4.810-MSWin32-x86
Upload. pl
Upload. config

The main execution logic is put in the Perl source file Upload. pl, And the configuration file is Upload. config.

The perl file automatically uploads all files in the folder to the specified directory of the specified ftp site according to the specified folder directory.

This Perl Script is actually inherited from Uwe Keim's Perl Script for uploading modified files to a FTP-Server,
It only adds the Fault Tolerance Response and reads the external configuration file, and clears the read/write access file that is irrelevant to the general business logic.

General procedure:
Step 1: log on to the ftp site;
Step 2: search for qualified files under the specified folder Class A and upload the Class A files to the specified directory on the FTP site;
Step 3: If all files in Class A are uploaded successfully, search for the specified file in Class B of the specified folder and upload the file to the specified FTP directory.
Step 4: Write the success/failure log.

Finally, the format of the success/failure log to be written is as follows:
Success: generate a log file named "Upload_Succ_2005_01_04_17_23.log ".
File Format: output the upload time, and the size and time of all uploaded file names.
Failed: generate a log file named "Upload_Fail_2005_01_04_17_23.log ".
File Format: output the upload time, the size and time of the uploaded file name, and the file name and cause of failure.

There are two ways to configure the perl script to run:
You can configure the time for running "Perl Upload. pl" in Windows scheduled tasks. You need to configure ActivePerl 5.8.4.810 in Windows;
You can also use Perl2Exe (p2x-8.40-Win32) to compile a perl script into an exe executable program that runs in a scheduled task (which requires PerlCRT. dll to be in the system path ).

[Note!] Before running, you must modify the "Upload. config" file to configure the required important parameters.
External configuration parameters

In the "Upload. config" configuration file in the same directory as the perl script, six external parameters are configured in advance:
Parameter 1: ftp_server:
The IP address of the FTP server.

Parameter 2: ftp_dir:
Specifies the FTP upload directory path;

Parameter 3: ftp_uid:
FTP login username;

Parameter 4: ftp_pw:
FTP login password;

Parameter 5: src_dir_WAVFiles, which is an array:
Specify the Class A folder and place all audio files to be uploaded;
Note: The subfolders in this directory will also be uploaded.

Parameter 6: src_dir_NamesListFile, which is an array:
Specify the Class B folder and place the Class B files.
Note: The subfolders in this directory will also be uploaded.

Appendix:
Upoad. pl content:

Copy codeThe Code is as follows :#! /Usr/bin/perl-w
##--------------------------------
#
# Project: FTP automatically uploads two types of files
#
# Module name: FTPAutoUpload
#
# Module task: automatically uploads all files in the specified folder to the specified directory of the specified ftp site according to the specified folder directory
#
# Program name: Upload. pl
#
##-------------------------------
##---------------------------#
# Referenced database Declaration 2
# Use strict;
Use File: Copy;
Use File: stat;
Use File: Find;
Use Net: FTP;
Use Date: pc1c qw (Delta_DHMS );
Use Date: Parse;
Use Win32: OLE;
Use Win32: OLE: Variant;
##---------------------------#
##---------------------------#
# Referenced database Declaration 1
#-Read the ini configuration file library
Use Config: IniFiles;
My $ cfg = Config: IniFiles-> new (-file => "Upload. config ");
##---------------------------#
##---------------------------#
# Read external parameters from the configuration file ##
##
# IP address of the FTP server ##
$ Ftp_server = $ cfg-> val ('ftpserver', 'ftp _ Server') | '';
# Specified FTP upload directory ##
#! Remember: do not add the "/" symbol at the end of the folder! #
$ Ftp_dir = $ cfg-> val ('ftpserver', 'ftp _ dir') | '';
# FTP login username ##
$ Ftp_uid = $ cfg-> val ('ftpserver', 'ftp _ uid') | '';
# FTP login password ##
$ Ftp_pw = $ cfg-> val ('ftpserver', 'ftp _ pw ') | '';
# Specify the "voice file" folder and place all audio files to be uploaded ##
#! Remember: do not add the "\" symbol at the end of the folder! #
@ Src_dir_WAVFiles = $ cfg-> val ('srcdirectory', 'src _ dir_wavfiles ');
# Specify the folder "Naming Convention list file TXT" and place the naming convention list file ##
#! Remember: do not add the "\" symbol at the end of the folder! #
@ Src_dir_NamesListFile = $ cfg-> val ('srcdirectory', 'src _ dir_nameslistfile ');
# A string set indicating which types of files/folders will not be uploaded to the server ##
@ Wc_exclude = ("_ vti", ". mdb", "\ bak", "\ data", "server. inc ");
##---------------------------#
##---------------------------#
# Prepare log files for recording the entire process
$ Logfilename = 'upload. log ';
$ Log_cnt = 0;
LOG ("");
LOG ("automatically upload FTP file Version 0.1 ");
LOG ("");
LOG ("Usage: Perl Upload. pl ");
LOG ("");
##---------------------------#
##---------------------------#
##=== Step 1 of program execution: log on to the ftp site ============================== ##
# $ Total_files indicates the number of uploaded files
$ Total_files = 0;
# $ Processed_files indicates the number of uploaded files.
$ Processed_files = 0;
# $ Skipped_files indicates the number of objects skipped
$ Skipped_files = 0;
# $ Start_date calculate the current Start Time
$ Start_date = timeString (time );
# $ G_nUploadSuccess indicates whether the upload is complete.-1 indicates no, and 1 indicates yes:
My $ g_nUploadSuccess = 1;
# $ G_nIsAllWAVsFile_UploadSuccess indicates whether Class A files have been fully uploaded.-1 is not, and 1 is:
My $ g_nIsAllWAVsFile_UploadSuccess = 1;
# $ G_strLastError:
My $ g_strLastError = "";
LOG ("connecting to the specified FTP Server ($ ftp_server )...");
$ Ftp = Net: FTP-> new ($ ftp_server );
If ($ @)
{
$ G_strLastError = "the FTP server cannot be connected. Error cause:". $ @;
LOG ("$ g_strLastError @ \ n ");
$ G_nUploadSuccess =-1;
}
Else
{
$ Ftp-> login ($ ftp_uid, $ ftp_pw );
If ($ @)
{
$ G_strLastError = "You cannot log on to the FTP server. Error cause:". $ @;
LOG ("$ g_strLastError \ n ");
$ G_nUploadSuccess =-1;
}
Else
{
$ Ftp-> binary;
LOG ("FTP server connected successfully! ");
##---------------------------#
##---------------------------#
##=== Step 2 of program execution, upload all Class A files in the specified class A folder to the directory specified by the FTP site == ##
My % lookup;
LOG ("prepare to upload all files under the" Class A Files "Directory (@ src_dir_WAVFiles! ");
Find (\ & processFiles, @ src_dir_WAVFiles );
LOG ("Directory (@ src_dir_WAVFiles) has been processed, and the result is :");
##---------------------------#
##=== Step 3 of program execution: Upload Class B files in the specified class B folder to the directory specified by the FTP site == ##
If ($ g_nIsAllWAVsFile_UploadSuccess> 0)
{
LOG ("+ = ");
LOG ("prepare to upload all files under the Class B directory (@ src_dir_NamesListFile! ");
Find (\ & processFiles, @ src_dir_NamesListFile );
LOG ("Directory (@ src_dir_NamesListFile) has been processed and the result is :");
LOG ("-= -");
}
Else
{
LOG ("-= -");
LOG ("because the directory of the Class A file is not completely uploaded, this class B file is not uploaded! ");
LOG ("-= -");
}
##---------------------------#
##---------------------------#
# The last log file is a statistical report
$ Span = calcDeltaSeconds ($ start_date, timeString (time ));
LOG ("Upload result: Successful. \ N: $ span seconds. A total of $ total_files are processed. $ processed_files is successfully uploaded and $ skipped_files are skipped. ");
$ Ftp-> quit () or warn "unable to quit: $ @ \ n ";
}
CloseLogfile ();
}
##---------------------------#
##---------------------------#
##=== Step 4 of Program Execution, write success log ================================ ##
If ($ g_nIsAllWAVsFile_UploadSuccess> 0 & $ g_nUploadSuccess> 0)
{
$ Logfilename = 'upload _ Succ _ '. Invalid timestring (time).'. log ';
$ Log_cnt = 0;
LOG ("");
LOG ("FTP Automatic File Upload Version 0.1 ");
LOG ("");
LOG ("Upload result: Successful. \ N: $ span seconds. A total of $ total_files are processed. $ processed_files is successfully uploaded and $ skipped_files are skipped. ");
LOG ("");
CloseLogfile ();
}
##---------------------------#
##---------------------------#
##=== Step 4 of Program Execution, write failure log ================================ ##
If ($ g_nIsAllWAVsFile_UploadSuccess <0 | $ g_nUploadSuccess <0)
{
$ Logfilename = 'upload _ Fail _ '. Invalid timestring (time).'. log ';
$ Log_cnt = 0;
LOG ("");
LOG ("FTP Automatic File Upload Version 0.1 ");
LOG ("");
LOG ("Upload result: failed. Cause of failure: $ g_strLastError. \ N: $ span seconds. A total of $ total_files are processed. $ processed_files is successfully uploaded and $ skipped_files are skipped. ");
LOG ("");
CloseLogfile ();
}
##---------------------------#
# The following is the definition of the sub-function body
##----------------------------##
##
# Function name: processFiles
# Functions:
# Obtain all the files and subfolders in the specified folder and process them in sequence.
##
# Programmer: Uwe Keim
##
# History:
# Date author remarks
#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 ("processing files $ 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 ("uploading files: from source \" $ src \ "to target \" $ dst \"...");
$ Ftp-> mkdir ($ dstdir, 1 );
$ Ftp-> put ($ src, $ dst) or $ g_nIsAllWAVsFile_UploadSuccess =-1;
If ($ g_nIsAllWAVsFile_UploadSuccess <0)
{
LOG ("files cannot be uploaded: from the source \" $ src \ "to the target \" $ dst \ "(dst-dir: \" $ dstdir \"). \ N ");
If ($ @)
{
LOG ("error cause: $ @ \ 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 partition timestring {
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, $ mon2, $ day2, $ hh2, $ mm2, $ ss2) = scanDate ($ t2 );
My ($ days, $ hours, $ minutes, $ seconds) = Delta_DHMS (
$ Year1, $ month1, $ day1, $ hh1, $ mm1, $ ss1, # earlier.
$ Year2, $ mon2, $ 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: "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 ("Log File opening error: File Name: $ logfilename; error cause: $! ");
};
Sub closeLogfile {
Close HLOG if defined HLOG;
}
Sub flushLogfile {
CloseLogfile ();
OpenLogfile ();
}
Appendix:
Upoad. config content:
# Configured external parameters ##
##
[FTPServer]
#-IP address of the FTP server -#
Ftp_server =
#-Specified FTP upload directory path -#
#! Remember: do not add the "/" symbol at the end of the folder! #
Ftp_dir =
#-FTP login username -#
Ftp_uid =
#-FTP login password -#
Ftp_pw =
# Configured external parameters ##
##
[SrcDirectory]
#-Specify the "voice file" folder and place all audio files to be uploaded -#
#! Remember: do not add the "\" symbol at the end of the folder! #
Src_dir_WAVFiles =
#-Specify the folder "Naming Convention list file TXT" and place the naming convention list file -#
#! Remember: do not add the "\" symbol at the end of the folder! #
Src_dir_NamesListFile =

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.