I am writing a script using ftp.exe to download files from an FTP server, it works at first. But the version I wrote was suited for only one file and the current date. My script is below:
echo user>>ftp.txt
echo password>>ftp.txt
set prefix=%date:~0,10%
set "name=%prefix%.txt"
echo get %name% >> ftp.txt
echo bye >> ftp.txt
ftp -s:ftp.txt ftpserver.com
del ftp.txt
But now there are more than one file named like aa-bb-2011-09-13.0.log,
aa-bb-2011-09-13.1.log,
aa-bb-2011-09-13.10.log. The last number is a serial number, it could be 0, 1, 2, 3…
How could download these files by batch script? How to modify my script to download more than one file (the number is unknown) which file name pattern is yesterday?
In terms of downloading multiple files, use
mgetinstead ofget. The former allows you to specify wildcards for getting rather than specific files.You’ll just have to construct the “name” with a wildcard pattern, and make sure you have a
promptin your script beforemgetotherwise it will ask for confirmation on every file.This is untested, but it’s probably as simple as changing:
to something like:
In terms of getting yesterdays date, you can use the following script. It’s pretty complex compared to what you would do in, for example
bash, but it works.It will set the
yesterdayenvironment variable to the formatYYYY-MM-DDso that you can use it in your current script. Simply invokecall yesterday.cmdand then use the environment variable.