If you need to download files regularly from an FTP server, Windows offers two onboard tools to automate this process. These are ftp.exe and curl, which was added just recently. For the latter, you only need one command; for FTP, you can use whole scripts.

A typical scenario for downloading files on a regular basis could be if you want to save a web server's backups locally. In this case, it is convenient if the whole process is performed without user intervention. Then, the FTP download could run automatically as a scheduled task.

Windows' own FTP client has been on board for ages, and it hasn't received an update since a long time. It therefore does not support encrypted transport, but only transfers as plain text. However, the tool can certainly be used in a secure network and for data that is not too critical.

Unlike FileZilla, Windows-FTP is a spartan program for the command line that can be automated with a sequence of commands from a text file. After establishing the connection and logging on to the server, you can basically pass all commands to it that are also allowed in interactive mode.

lcd C:\Users\me\Downlaods
open ftp.myserver.com
<username>
<Password>
prompt
binary
mget *.gz
mdelete *.gz
quit

This example first sets the user's download folder as the local working directory, connects to the server, and logs in with the user's name and password.

It makes sense to deactivate the interactive mode with prompt; otherwise, the script hangs when multiple downloads must be confirmed.

With binary, you switch from the standard ASCII to binary mode, which is appropriate for ZIP archives.

If you are not in the correct remote directory, you can change to that directory with cd. The download is then done with get, or mget if you want to download multiple files. The latter supports wildcards, so enter the following command in the above file:

mget *.gz

If you want to delete the files after the transfer, then the following applies:

mdelete *.gz

End the session with quit.

Automated FTP download using a script

Automated FTP download using a script

If you have saved all commands in a text file, for example, with the name autoftp.txt, then start the download with

ftp.exe -s:autoftp.txt

FTP download with curl ^

Since Windows 10 1809, Microsoft has included an implementation of curl with the operating system. However, if you want to start it under PowerShell using just "curl", you will launch Invoke-WebRequest, because such an alias is defined by default. So you have to enter curl.exe instead.

The utility supports several protocols, including FTP and, unlike ftp.exe, also FTPS (over TLS). If you specify the --ftp-ssl switch, then it will try to establish a secure connection and will fall back to FTP if this fails. You can force FTPS with --ssl-reqd.

The automatic login is achieved with the parameter -u username:password or --user username:password followed by the path to the desired file:

curl.exe -u ftp:ftp -O ftp.gnu.org/gnu/wget/wget2-latest.tar.gz

The -O switch tells curl to save the file under the same name. If you want a different filename as the destination, you can achieve it like this:

curl.exe -u ftp:ftp ftp.gnu.org/gnu/wget/wget2-latest.tar.gz -o wget.tar.gz

To download arbitrary files from a directory, curl is less flexible than FTP; at least, the implementation for Windows is. You can display the contents of a directory by appending a trailing slash:

curl ftp://ftp.gnu.org/gnu/wget/ -u ftp:ftp

But this does not help to automate the download because curl lacks the support of wildcards if the file names change regularly (for example, with backups, whose names usually contain the date).

FTP download with curl subsequent listing of directory contents

FTP download with curl subsequent listing of directory contents

Conclusion ^

FTP is a long-serving method for file transfer and offers the possibility of automating processes using commands that are simply stored line by line in a text file. However, the Windows client does not support secure connections, so you have to limit its use to environments where this is not a problem.

Subscribe to 4sysops newsletter!

Curl is a newcomer to the Windows world, offering a variety of options and dealing with multiple protocols. However, it is less flexible for automating FTP downloads.

avatar
0 Comments

Leave a reply

Your email address will not be published.

*

© 4sysops 2006 - 2022

CONTACT US

Please ask IT administration questions in the forums. Any other messages are welcome.

Sending

Log in with your credentials

or    

Forgot your details?

Create Account