I'm using following FTP script on windows xp to download zip files from Ubuntu cloud servers. A zip file is created every day on Ubuntu servers and I will download it to windows via this ftp script. I run this script everyday manually as I have to edit the last line(mget /usr/backup_02-11-2010.zip) of the script to match today's date. I want to edit this script so that it will download only today's zip file at the scheduled time without needing to edit it everyday, when scheduled. It's clear that date is appended to the zip files and is in the format dd-mm-yyyy. Need help...

The file one.txt:

open server-ip-here
lcd C:\Backup\files
mget /usr/backup_02-11-2010.zip

The file one.bat:

ftp -s:one.txt

I will be executing one.bat file.

  • why my question is down voted?. Isn't it useful to atleast one – user3215 Feb 1 '11 at 2:33

Not really the place to ask about Windows scripting, but here you go. Add this to the top of one.bat, to rewrite the script file each time. This assumes your Windows regional settings are also set to dd/mm/yyyy.

for /f "tokens=1-3 delims=/" %%a in ('date /t') do set dashdate=%%a-%%b-%%c
set dashdate=%dashdate: =%
echo open server-ip-here > one.txt
echo username-here >> one.txt
echo mget /usr/backup_%dashdate%.zip >> one.txt
  • it just flashes and goes. – user3215 Nov 8 '10 at 14:04
  • I think you need to add the line in the end of this batch file: ftp -s:one.txt – LFC_fan Nov 8 '10 at 14:25
  • Yep, you do need to add this to the top of one.bat, leaving your existing line at the bottom. – misterben Nov 8 '10 at 15:11

I can't see a way of doing this with dos because the date command outputs as 08/11/2010 instead of 08-11-2010. If you can install powershell, you could use this to get the correct date format

get-date -uformat "%d-%m-%y"

and then append that to the command string.

Hope this helps, Bodsda


Just change your one.txt file as below:

set ctime=%date:~-7,2%-%date:~-10,2%-%date:~4%
open server-ip-here<br />
username-here<br />
user-password-here<br />
lcd C:\Backup\files<br />
bin<br />
hash<br />
prompt  <br />
mget /usr/backup_%cdate%.zip<br />

From a unix environment I'd just SSH/SCP and some bash magic instead of FTP. Windows itself won't help you out much there and its limited scripting environment makes me cry a little but if you could install something like Cygwin (or run Linux in the background in a VM) your whole thing can be boiled down to:

scp user@host:/usr/`ssh user@host 'ls -1r /usr/backup_* | head -1'` /local/path

Obviously sub out user and host for real values.

I suggest using public key authentication so there's no password prompt but if you're find typing in the password a couple of times, this should do.


If you are willing to use cygwin, you can also use wget and the following command to get the download through FTP.

wget --user=username --password=password --directory-prefix=target-directory ftp://url/path_to_directory/backup_`date +%d-%m-%Y`.zip

Here it worked and I copied from each answers by experts to make it work:

@REM Beginning of one.bat
    @Echo Off
    @set mydate=%date:~-7,2%-%date:~-10,2%-%date:~-4%

    @REM Next write the FTP commands into one.txt

    @echo open server-ip-here> one.txt
    @echo username-here>> one.txt
    @echo password-here>> one.txt
    @echo lcd G:\Backup\files>> one.txt
    @echo bin>> one.txt
    @echo hash>> one.txt
    @echo prompt>> one.txt
    @echo get /usr/backup_%mydate%.zip>> one.txt

    @REM Finally run the FTP command with the one.txt file
    ftp -s:one.txt

    @REM End of one.bat

There should not be any space before > or >> in the script and in my case the script was failing as I had spaces before them and I was trying this for long.

The script creates a file called one.txt, the first line ...> one.txt overwrites the one.txt file and the remaining appends to one.txt.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.