Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. 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 ubutnu 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...

open server-ip-here
username-here
user-password-here
lcd C:\Backup\files
bin
hash
prompt  
mget /usr/backup_02-11-2010.zip
share|improve this question

2 Answers 2

up vote 4 down vote accepted

Add a variable containing your desired date format to your script:

set mydate=%date:~0,2%-%date:~3,2%-%date:~6,4%

This will echo the current date in format dd-mm-yyyy. Then you can use the variable mydate to get the actual file:

get /usr/backup_%mydate%.Zip
share|improve this answer
1  
You only need to use mget if you have a wildcard in the filename, like mget backup*.Zip - normally I'd use plain get for an exact filename. –  RedGrittyBrick Nov 3 '10 at 10:46
    
Thanks, will update this. –  weeheavy Nov 3 '10 at 10:52
    
I'm not sure where to exactly append "set mydate=%date:~-10,2%-%date:~-7,2%-%date:~-4%" in the script. I added this after the line 'lcd' and its connected but did not do anything. –  user53864 Nov 4 '10 at 6:47
    
I'm getting the error like the following: ftp> get /usr/backup_%mydate%.Zip 200 PORT command successful. Consider using PASV. 550 Failed to open file. –  user53864 Nov 4 '10 at 6:59
1  
Useless comment here, but man does that syntax for date suck on Windows! It's a wonder that people even bother scripting with that. date +"%d-%m-%Y" ftw –  hobodave Jan 3 '11 at 8:28

Here it worked and I copied from the expert 'Weeheavy' 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
    

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.