FTP from the Windows XP command line

FTPSometimes I discover some new… old part of Windows ­čśÇ
I did some research on Google to learn how to download a file daily, via FTP, and now I know that Windows has a little FTP client (named, with a lot of fancy, ftp.exe) that can do everything a “normal” FTP client like FileZilla or CuteFTP can do.

It hasn’t a graphical interface, the only way to use it is from the command line, and so it’s perfect to be scheduled, to execute repeated tasks. This is how I programmed Windows to accomplish my need:

– I created a new text file (next I renamed it “ftp.bat“)┬áwith the following text:
echo open ftp.mydomain.com >> D:\Backup\script.txt
echo mylogin >> D:\Backup\script.txt
echo mypassword >> D:\Backup\script.txt
echo lcd D:\Backup >> D:\Backup\script.txt
echo prompt >> D:\Backup\script.txt
echo mget %DATE:~8,2%%DATE:~3,2%%DATE:~0,2%*.psc >> D:\Backup\script.txt
echo bye >> D:\Backup\script.txt
echo ftp -s:D:\Backup\script.bat >> D:\Backup\script.txt

C:\WINDOWS\system32\ftp.exe -s:D:\Backup\script.txt
del D:\Backup\script.txt

This script connect to ftp.mysite.com (just change the address to any other FTP server you want), sends login and password, changes (with “lcd”) the local work path to D:\Backup and┬ádisables the prompt to void confirmations;

then it downloads (mget) a file with a variable name, for example 060725050315.psc (this is the daily backup I would like to download). My additional problem was that I knew the first part of the file name (060725… it’s today date!) and its extension (.psc), but not the hour (in the example, 050315), because it indicates the hour when the remote backup ended. So I used a little Windows batch script that extracts current year, month and day (DATE:~8,2, DATE:~3,2 and DATE:~0,2) from my system’s local time (it’s in italian format, DD/MM/YYYY),┬áthen a “*” wildcard character and the fixed extension.
So I’m sure to download the today backup(s), regardless of the hour…

at the end, it disconnects (“bye”).

Because of the variable date problem (see above), the whole script first writes updated FTP commands in a temporary file (D:\Backup\script.txt), then it call ftp.exe with the new .txt file as argument. Finally, it delete the text file.

The last step I did was to add my .bat file to the Windows’ scheduled tasks, to make it run daily, at 5.15 AM ­čśë

This entry was posted in Tricks. Bookmark the permalink. Trackbacks are closed, but you can post a comment.