Using DOS batch processing to implement FTP automatic upload, download, and file cleanup seems very busy recently. I haven't been writing anything for a long time, today, I wrote a program that uses the batch processing program to automatically upload, download, and clear files through FTP. Record it for future use.
Function Description: Automatically log on to the remote host and download files to the specified directory. After the download is complete, check the files downloaded to the local host and delete the files on the host.
Program name and function:
GetFile. BAT: main program, check the configuration directory and call the FTP script.
GetFile. FTP: ftp run script.
Movefile. BAT: delete files on the remote host.
The specific content of batch processing is as follows. 1. GetFile. Bat content:
@ Echo off
Rem configure the basic drive letter, home directory and file storage directory
Set base = D:
Set base_path = D: \ doc \ card \ ftp
SET LOG_FILE = % base_path % \ ftpgetfile. Log
Set flag_file = % base_path % \ TMP. TMP # because it is required to communicate with another program, the State mark file is used. When the program starts, enter 0, and then modify it to 1 at the end. In this way, another program can determine when the file is received and start to process the file.
Set save_path = % base_path %
Set tmp_path = % base_path % \ Temp
Set remote_path =/home/ecton/data/downloads/1234
Rem program starts running
Echo ===================================================== ==================================>> % LOG_FILE %
Set datet = % date % time %
Echo program starts running. Time: % datet %> % LOG_FILE %
% Base %
If not exist % base_path % (
Mkdir % base_path %
)
If not exist % save_path % (
Mkdir % save_path %
) If not exist % tmp_path % (
Mkdir % tmp_path %
)
Cd % tmp_path %
Echo 0> % flag_file %
If not exist % save_path % (
Echo cannot find the flag file % save_path %> % LOG_FILE %
Goto _ end
)
FTP-n-s: "% base_path % \ GetFile. FTP"> % LOG_FILE %
Echo: The obtained file >>% LOG_FILE %
DIR/B % tmp_path % \ s *. EDB> % tmp_path % \ filelist. tmp # list of files received this time for movefile. BAT to delete the file ID
DIR/B % tmp_path % \ *. EDB> % LOG_FILE %
Move/Y % tmp_path % \ *. EDB % save_path % \
For/F "usebackq delims =" % I in ('Type filelist. tmp ') Do call % base_path % \ movefile. Bat % I % LOG_FILE % remote_path %
: _ End
Echo 1> % flag_file %
Del % tmp_path % \ filelist. tmp
Set datet = % date % time %
Echo program running ended. Time: % datet % >>% LOG_FILE %
Echo ===================================================== ==================================>> % LOG_FILE %
Exit2.getfile. FTP file content:
Open 192.168.0.208
User ecton
Huateng
CD/home/ecton/data/downloads/1234
Prompt off
Mget *. EDB # other operations such as put and mput files can also be added.
CD/home/ecton/data/download/pub
Mget *. EDB
Bye
Quit
Exit3.movefile. BAT file content:
Echo open 192.168.0.208> ftp.txt
Echo user ecton Huateng> ftp.txt
Echo cd % 3> ftp.txt
Echo prompt off> ftp.txt
Echo Delete % 1> ftp.txt # Here is the delete action, you can also rename
Echo bye> ftp.txt
Echo quit> ftp.txt
Echo exit> ftp.txt
FTP-n-s: ftp.txt> % 2del ftp.txt because the parameter cannot be directly transmitted to the FTP Script Function in Windows, you have to come up with such a stupid way to use GetFile. bat transfers the file name to movefile. BAT, and then by movefile. bat dynamically generates and executes an FTP script.