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!
Using only one (1) .bat file script. Create the FTP script in a temporary file, run it, then delete the temporary file.
Windows has the at utility as well as the Windows task scheduler. Either one can run your program at a specified interval.
To program ftp from a batch file, see http://support.microsoft.com/kb/96269. You need to call ftp like this
where ftpcommands.txt looks something like this:
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 aprompt off
command to the commands text file before themget
. Without this,mget
will stop and ask you to confirm before getting each file. [Thanks to Adriano for pointing this out!])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
):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)