Windows Bat script implements FTP automatic download removal

Source: Internet
Author: User
Tags tmp file

Now there is a need to take a remote file below the image, download to the local, and delete the successful download of the file, and the remote directory of the image will be added at any time. Suppose that if all the scripts are written, then it is OK to call the scheduled task on Windows and execute the script on time, and Linux can use Cronjob as well. The general realization of the idea is as follows, of course, more than this one way.

    1. Build the FTP server on the remote server, such as the images on the remote service as the download directory. The simplest way to build FTP server is to use the FileZilla Server tool to build, very convenient, 0 configuration.
    2. Script on the client side, the first implementation can be downloaded from the remote server, and then download the successful file to the remote server to delete, here the details need to note:

A. First write the list of downloaded files to a temporary file such as file filelist.tmp, while moving files downloaded to a temporary directory to a formal directory such as the download directory

B. Then delete the file in accordance with the file list in this filelist.tmp file, because Windows can not directly pass parameters to the FTP script function, so delete files are deleted in batches, such as delete 10 files at a time, of course, you can control your own chores, Here the main use of the FTP command Mdelete this method, while you can delete multiple files.

3. Then add the script to the task plan according to your needs.

This program contains 2 scripts, Getfile.bat and Removefile.bat

The code for Getfile.bat is as follows:

@echo Offrem Configuration Basic drive letter, home directory and file directory set base=D:set Base_path=d:\bat\FTPSet Log_file=%base_path%\ftpgetfile.logset Flag_file=%base_path%\tmp.tmpset SA~ve_path=%base_path%\downloadset Tmp_path=%base_path%\temprem remote directory set Remote_path=/imagesREM program starts runningEcho=========================================================== >>%log_file%Set MyDate=%Date:~0,Ten% % Time:~0,8%Echo "Program is running start time:%mydate%">>%log_file%%base%ifNot exist%base_path% (  mkdir%base_path%)ifNot exist%sa~ve_path% (  mkdir%sa~ve_path%)ifNot exist%tmp_path% (  mkdir%tmp_path%) CD%tmp_path%the REM program is set before the flag bit is0Echo 0>%flag_file%ifNot exist%sa~ve_path% (EchoUnable to find the flag file%sa~ve_path% >>%log_file%goto _end)FTP-S:"%base_path%\getfile.ftp">>%log_file%EchoThe documents obtained are as follows:>>%log_file%dir/b%tmp_path%\*.* >%base_path%\filelist.tmp setlocal enabledelayedexpansionset Count=0 for/F"delims="%%i in (%base_path%\filelist.tmp) Do (set/A count+=1)if%count% = =0 (    Echo "NO files need download!">>%log_file%exit)dir/b%tmp_path%\*.* >>%log_file%REM copy downloaded files to other directory move/y%tmp_path%\*.*%sa~ve_path%Set num=1set FileList= for/F"delims="%%i in (%base_path%\filelist.tmp) Do (set filelist=!filelist! %%I REM delete set with 5 files for a group/tmp =!num! %%5    if!tmp! ==0( call%base_path%\removefile.bat"!filelist!"%log_file%%remote_path%set FileList=    ) Else(REM If the total number of files is equal to the number of NUM and cannot reach 5 files as a group, then it is deleted directlyif!count! LEQ!num!( call%base_path%\removefile.bat"!filelist!"%log_file%%remote_path%set FileList=)) Set/A num+=1) The Endlocal REM program executes after the set flag bit is 1Echo 1>%flag_file%REM del%base_path%\filelist.tmp Delete the downloaded file list set mydate=%Date:~0,Ten% % Time:~0,8%Echo=========================================================== >>%log_file%Echo "Program is running end:%mydate%">>%log_file%Exit

This script requires a login to the FTP server information,getfile.ftp the content under:

Open your FTP server IP USERPASSWORDCD  your ftp floderprompt offmget *.*byequitexit

The contents of the Removefile.bat script are as follows:

EchoOpen121.40.123.74>FTP. txtEchoMht.cherry>>FTP. txtEchoMht.cherry>>FTP. txt
REM% 1 for the first parameter% 2 for the second argument and so onEchocd%3>>FTPThe Txtrem is mostly disposed of here.fileset TMP for front and back double quotes in list=%1Set"tmp=%tmp:"=%"RemEchoPrompt off>>FTP. txt REM mdelete deleted format Mdelete1. txt2. txt3. txtEchoMdelete%tmp%>>FTP. txtEchoBye>>FTP. txtEchoQuit>>FTP. txtEchoExit>>FTP. txtFTP-i-s:FTP. txt >>%2delFTP. txt

Here, the functionality has been completed. The next step is to add the Getfile.bat script to the Windows scheduled task, which I do not explain here, if not familiar with the online search to see.

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.