Copy files from FTP server to local directory?

2019-01-14 19:23发布

I want to create a batch file, including the following functions:

  • Connection to a FTP server
  • Copying the files from there (directory called "out") to a local directory
  • if success, then deleting the files from the FTP server
  • repeating those steps every 15 minutes

I haven't done that much with batch files so far, so it would be great if you could help me. I know there is the ftp command, and I know how to connect (ftp open), but unfortunately I don't know how to copy those files from there every 15 minutes.

Thanks a lot for your help!

4条回答
Fickle 薄情
2楼-- · 2019-01-14 19:54

Using only one (1) .bat file script. Create the FTP script in a temporary file, run it, then delete the temporary file.

SET "FTPFILE=%TEMP%\myftpscript_%RANDOM%.txt"

ECHO>>"%FTPFILE%" open ftp.myftpsite.com
ECHO>>"%FTPFILE%" username
ECHO>>"%FTPFILE%" password
ECHO>>"%FTPFILE%" bin
ECHO>>"%FTPFILE%" cd out
ECHO>>"%FTPFILE%" mget *
ECHO>>"%FTPFILE%" del *
ECHO>>"%FTPFILE%" bye

ftp -i -s:"%FTPFILE%"

IF EXIST "%FTPFILE%" (DEL "%FTPFILE%")

EXIT /B 0
查看更多
聊天终结者
3楼-- · 2019-01-14 20:05

Windows has the at utility as well as the Windows task scheduler. Either one can run your program at a specified interval.

查看更多
倾城 Initia
4楼-- · 2019-01-14 20:10

To program ftp from a batch file, see http://support.microsoft.com/kb/96269. You need to call ftp like this

ftp -i -s:ftpcommands.txt

where ftpcommands.txt looks something like this:

open ftp.myftpsite.com
username
password
bin
cd out
mget *
del *
bye

For running this every 15 minutes, see other replies (at or Command Scheduler).

(The -i parameter is to turn off interactive prompting - the other way to do this is to add a prompt off command to the commands text file before the mget. Without this, mget will stop and ask you to confirm before getting each file. [Thanks to Adriano for pointing this out!])

查看更多
做个烂人
5楼-- · 2019-01-14 20:20

The accepted answer by @AAT suggests using Windows built-in ftp.exe command-line client. While that can work, more often it won't, because this client does support FTP active mode only, which does not play nicely with today's ubiquitous firewalls and NATs. It also does not support encrypted FTPS (FTP over TLS/SSL).

If you have a problem with the above, you need to use a 3rd party FTP client. Most of them do support both the passive mode and encryption.


For example with WinSCP FTP client, you can use the following batch file (.bat):

WinSCP.com /command ^
    "open ftp://username:password@ftp.example.com/" ^
    "get /out/* c:\local\path\" ^
    "exit"

In case you already have an ftp.exe script, there's a guide for converting it to WinSCP script.


For the scheduling part, see the guide to scheduling transfers to FTP server.


(I'm the author of WinSCP)

查看更多
登录 后发表回答