Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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
username-here
user-password-here
lcd C:\Backup\files
bin
hash
prompt  
mget /usr/backup_02-11-2010.zip

The file one.bat:

ftp -s:one.txt

I will be executing one.bat file.

share|improve this question
    
why my question is down voted?. Isn't it useful to atleast one –  user3215 Feb 1 '11 at 2:33
add comment

6 Answers

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
[etc...]
echo mget /usr/backup_%dashdate%.zip >> one.txt
share|improve this answer
    
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
add comment

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

share|improve this answer
add comment

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 />
share|improve this answer
add comment

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.

share|improve this answer
add comment

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
share|improve this answer
add comment
up vote 0 down vote accepted

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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