FTP automatically upload files to Perl scripts and configuration Files _perl

Source: Internet
Author: User
Tags file upload ftp file ftp login ftp site ole sprintf win32 perl script
-->

Several basic concepts of this application design are:
Toolbox reuse: Use existing tools as much as possible;
Simplify the running steps; without introducing too much business logic, the more satisfying the requirements are, the better.

So, we've defined the operation of this application depending on the following tools:
Activeperl-5.8.4.810-mswin32-x86
upload.pl
Upload.config

We put the main execution logic in the Perl source file upload.pl, and the configuration file is Upload.config.

This Perl file will perform the task of automatically uploading all files under that folder to the specified FTP site in the specified directory, according to the folder directory.

This Perl script is actually inherited from Uwe Keim's "Perl Script for uploading modified files to a Ftp-server",
It simply adds a fault-tolerant response and reads the parts of the external configuration file, and then digs out the parts of the access file that are not related to general business logic.

Program General Process:
The first step, try to login FTP site;
The second step is to find the eligible files under the specified folder a category and upload the Class A file to the FTP site designated directory;
Third, if the Class A files are uploaded successfully, look for the specified file under the specified folder Type B and upload it to the FTP specified directory
The fourth step is to write a success/failure log.

Finally, the format of the success/failure log to write is as follows:
Success: Generate a log file named "Upload_succ_2005_01_04_17_23.log"
File format: Output upload time, and all uploaded file names and their size and time spent.
Failure: Generates a log file named "Upload_fail_2005_01_04_17_23.log"
File format: Output upload time, as well as uploaded file names and their size and time spent, and the failure of the file name and reason.

There are two ways to configure Perl scripts to run:
You can configure the time to run "Perl upload.pl" in the Windows scheduling task, which requires configuring ActivePerl 5.8.4.810 in the Windows environment;
You can also use Perl2exe (P2X-8.40-WIN32) to compile Perl scripts into an EXE executable program that runs this EXE in a scheduled task (which requires PerlCRT.dll under the system path).

Attention Before running, you must modify the "Upload.config" file to configure the important parameters that you want.
External configuration parameters

In the "upload.config" configuration file in the same directory as the Perl script, there are six external parameters that were configured beforehand:
Parameter 1:ftp_server:
The IP address of the FTP server.

Parameter 2:ftp_dir:
The specified FTP upload directory path;

Parameter 3:ftp_uid:
FTP login user name;

Parameter 4:FTP_PW:
FTP login password;

Parameter 5:src_dir_wavfiles, which is an array:
Specify a Class A folder, place all the audio files to upload;
Note: Subfolders under this directory will also be uploaded.

Parameter 6:src_dir_nameslistfile, which is an array:
Specify Class B folders, and place class B files.
Note: Subfolders under this directory will also be uploaded.

Appendix:
upoad.pl content:

Copy Code code as follows:

#!/usr/bin/perl-w
##--------------------------------
#
# project: FTP automatically upload two types of files
#
# Module Name: ftpautoupload
#
# Module Task: Automatically upload all files under this folder to the specified FTP site in accordance with the specified folder directory
#
# program Name: upload.pl
#
##-------------------------------
##---------------------------#
# # referenced Library declaration 2
#use Strict;
Use file::copy;
Use File::stat;
Use File::find;
Use net::ftp;
Use Date::P calc QW (DELTA_DHMS);
Use Date::P arse;
Use Win32::ole;
Use win32::ole::variant;
##---------------------------#
##---------------------------#
# # referenced Library Declaration 1
#-read the INI configuration file Library
Use Config::inifiles;
My $cfg = config::inifiles->new (-file => "Upload.config");
##---------------------------#
##---------------------------#
# # Read external parameters from configuration file # #
##
# # IP Address of FTP server # #
$ftp _server = $cfg->val (' ftpserver ', ' ftp_server ') | | '';
# # FTP upload directory path specified # #
#! Remember: The folder does not add the "/" symbol at the end!#
$ftp _dir = $cfg->val (' ftpserver ', ' Ftp_dir ') | | '';
# # FTP Login user name # #
$ftp _uid = $cfg->val (' ftpserver ', ' ftp_uid ') | | '';
# # FTP Login Password # #
$ftp _PW = $cfg->val (' ftpserver ', ' FTP_PW ') | | '';
# # Specify folder ' Voice files ', place all audio files to upload # #
#! Remember: The folder does not add the "\" symbol at the end!#
@src_dir_WAVFiles = $cfg->val (' srcdirectory ', ' src_dir_wavfiles ');
# # Specify folder ' named Control List file txt ', place name control List File # #
#! Remember: The folder does not add the "\" symbol at the end!#
@src_dir_NamesListFile = $cfg->val (' srcdirectory ', ' src_dir_nameslistfile ');
# # A collection of strings indicating which types of files/folders will not be uploaded to the server # #
@wc_exclude = ("_vti", ". mdb", "\\bak", "\\data", "server.inc");
##---------------------------#
##---------------------------#
# # log file preparation for all procedures
$logfilename = ' Upload.log ';
$log _cnt = 0;
LOG ("");
LOG ("Automatically upload ftp file Version 0.1");
LOG ("");
LOG ("Usage: Perl upload.pl");
LOG ("");
##---------------------------#
##---------------------------#
The first step in ##=== program execution: Try to log on to the FTP site ==========================##
# # $total _files is the number of uploaded files
$total _files = 0;
# # $processed _files is the number of uploaded files
$processed _files = 0;
# # $skipped _files is the number of skipped files
$skipped _files = 0;
# # $start _date to calculate the current start time
$start _date = timestring (time);
# # $g Whether the _nuploadsuccess representative has been fully uploaded,-1 is not, 1 is:
My $g _nuploadsuccess = 1;
# # $G _nisallwavsfile_uploadsuccess Representative has completely uploaded a-class file,-1 is not, 1 is:
My $g _nisallwavsfile_uploadsuccess = 1;
# # $g _strlasterror represents the last error reason:
My $g _strlasterror = "";
LOG ("is linking to the specified FTP server ($ftp _server) ...");
$ftp = net::ftp->new ($ftp _server);
if ($@)
{
$g _strlasterror = "Cannot connect to FTP server for error reason:". $@;
LOG ("$g _strlasterror@\n");
$g _nuploadsuccess =-1;
}
Else
{
$ftp->login ($ftp _uid, $ftp _PW);
if ($@)
{
$g _strlasterror = "Cannot login FTP server, error Reason:". $@;
LOG ("$g _strlasterror\n");
$g _nuploadsuccess =-1;
}
Else
{
$ftp->binary;
LOG ("link FTP server successful!");
##---------------------------#
##---------------------------#
The second step of the ##=== program is to upload all Class A files under the Class A folder to the FTP site specified directory ===##
My%lookup;
LOG ("Ready to upload all files under the Category A Files" directory (@src_dir_WAVFiles)! ");
Find (\&processfiles, @src_dir_WAVFiles);
LOG ("directory (@src_dir_WAVFiles) has been processed, the result is:");
##---------------------------#
The third step of the ##=== program is to upload the B-class file Under the specified B-class folder to the FTP site specified directory ===##
if ($g _nisallwavsfile_uploadsuccess > 0)
{
LOG ("+===============================+");
LOG ("Ready to upload all files under Category B directory (@src_dir_NamesListFile)!");
Find (\&processfiles, @src_dir_NamesListFile);
LOG ("directory (@src_dir_NamesListFile) has been processed, the result is:");
LOG ("-===============================-");
}
Else
{
LOG ("-===============================-");
LOG ("Because the Category A file directory is not fully uploaded, so this B-class file does not upload!");
LOG ("-===============================-");
}
##---------------------------#
##---------------------------#
# The last of the log files is a statistical report
$span = Calcdeltaseconds ($start _date,timestring (time));
LOG ("Upload result: successful.") \ n Cost: $span seconds, a total of $total _files files were processed, in which $processed _files uploaded successfully, skipping $skipped _files files. ");
$ftp->quit () or warn "Unable to quit: $@\n";
}
Closelogfile ();
}
##---------------------------#
##---------------------------#
The fourth step of ##=== program execution, write success log ===============================##
if ($g _nisallwavsfile_uploadsuccess > 0 && $g _nuploadsuccess > 0)
{
$logfilename = ' upload_succ_ '. shorttimestring (Time). Log ';
$log _cnt = 0;
LOG ("");
LOG ("FTP automatic upload file Version 0.1");
LOG ("");
LOG ("Upload result: successful.") \ n Cost: $span seconds, a total of $total _files files were processed, in which $processed _files uploaded successfully, skipping $skipped _files files. ");
LOG ("");
Closelogfile ();
}
##---------------------------#
##---------------------------#
The fourth step of ##=== program execution, write failure log ===============================##
if ($g _nisallwavsfile_uploadsuccess < 0 | | $g _nuploadsuccess < 0)
{
$logfilename = ' Upload_fail_ '. shorttimestring (Time). Log ';
$log _cnt = 0;
LOG ("");
LOG ("FTP automatic upload file Version 0.1");
LOG ("");
LOG ("Upload result: Failed.") Reason for failure: $g _strlasterror. \ n Cost: $span seconds, a total of $total _files files were processed, in which $processed _files uploaded successfully, skipping $skipped _files files. ");
LOG ("");
Closelogfile ();
}
##---------------------------#
# # Below is the definition of the child function body
##----------------------------##
##
# # Function Name: processfiles
# # Features:
# # Get all the files and subfolders under the specified folder, and then process them sequentially.
##
# # Programmer: Uwe Keim
##
# # History:
# # Numbered Date author notes
# # 1 Uwe Keim

##
##----------------------##
Sub Processfiles
{
My $srcdir = Fstobs ($File:: Find::d ir);
My $srcpath = Fstobs ($File:: Find::name);
My $base = Fstobs ($File:: Find::topdir);
foreach my $exclude (@wc_exclude) {
if (Index ($srcpath, $exclude) >-1) {
$File:: find::p rune = 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 file $total _files \" $src \ "...");
# --------------------
# check time.
My $need _upload = 0;
# Create time.
My $t 1 = $lookup {$SRC};
My $t 2 = timestring (stat ($SRC)->mtime);
if (not defined $t 1) {
$lookup {$SRC} = $t 2;
$need _upload = 1;
} else {
My $delta _sec = Calcdeltaseconds ($t 1, $t 2);
$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 ("Cannot upload file: from source \ $src \" to target \ "$DST \" (Dst-dir: \ "$dstdir \"). \ n ");
if ($@)
{
LOG ("Error Reason: $@\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 parameter, later as second.
Sub Calcdeltaseconds {
My ($t 1, $t 2) = @_;
My ($year 1, $month 1, $day 1, $hh 1, $mm 1, $ss 1) = ScanDate ($t 1);
My ($year 2, $month 2, $day 2, $hh 2, $mm 2, $ss 2) = ScanDate ($t 2);
My ($days, $hours, $minutes, $seconds) = Delta_dhms (
$year 1, $month 1, $day 1, $hh 1, $mm 1, $ss 1, # earlier.
$year 2, $month 2, $day 2, $hh 2, $mm 2, $ss 2); # 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 ("Error opening log file: file name is $logfilename; Error reason: $!");
};
Sub Closelogfile {
Close hlog if defined hlog;
}
Sub Flushlogfile {
Closelogfile ();
OpenLogFile ();
}
Appendix:
Upoad.config content:
# # Configuration External Parameters # #
##
[Ftpserver]
#-FTP Server IP address-#
Ftp_server =
#-Specifies the FTP upload directory path-#
#! Remember: The folder does not add the "/" symbol at the end!#
Ftp_dir =
#-FTP login Username-#
Ftp_uid =
#-FTP Login Password-#
FTP_PW =
# # Configuration External Parameters # #
##
[Srcdirectory]
#-Specifies the folder "Voice files" and places all the audio files to be uploaded-#
#! Remember: The folder does not add the "\" symbol at the end!#
Src_dir_wavfiles =
#-Specify folder "Name control List file txt", place named Control List file-#
#! Remember: The folder does not add the "\" symbol at the end!#
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.