Found several solutions for local, but need one for FTP action:
I have a daily batch script to backup MySQL databases locally, however I also ftp them to a backup server.
Locally, I am using forfiles
to delete files older than 14 days:
forfiles -p "C:\whatever" -s -m *.* -d 14 -c "cmd /c del @path"`
The same I would like to do on FTP, once the newest backup files are dumped into FTP server running on Windows Server 2008 R2.
How do I extend my batch file to do this action?
cd\[path to directory where files are saved]
echo off
echo user [ftp username]>ftpup.dat
echo [ftp password]>>ftpup.dat
echo binary>>ftpup.dat
echo put [FullBackup.%backupdate%.zip]>>ftpup.dat
echo quit>>ftpup.dat
ftp -n -s:ftpup.dat [myserver.com]
del ftpup.dat
capture the list of files you are interested in
prepare the ftp delete script
process the list
inside the loop, extract the file date.
And transform the month name into a number
you may use the routine found in http://www.dostips.com/DtTipsStringManipulation.php
inside the loop, transform the date into a number of days since today
this is a call to a routine that computes the days using the Fliegel-Van Flandern algorithm. See this SO question for details and implementation How can I check the time stamp creation of a file in a Windows batch script?
and finally, inside the loop, compare the resulting number, if it is bigger than, say, 14, add the file to the files to delete ftp script
finish the ftp del script and execute it
It's cumbersome to implement with the built-in
ftp.exe
, as the very good answer by @PA. shows.You better use some more powerful scriptable 3rd party FTP client.
For example with WinSCP FTP client, it's as trivial as:
See file masks with time constraints.
(I'm the author of WinSCP)