If you’ve read this post of mine you already know about FTPUSE: a great command-line freeware tool that we can use to map a FTP server or folder to a Windows drive letter. Think of it as a Map Network Drive that accepts FTP addresses instead of UNC shares. Such powerful feature can be used to automate a lot of tasks that we usually can’t do within FTP folders, such as – for example – deleting all the files older than X days.
Here’s how we can do that with the help of FTPUSE, assuming we have it installed:
: delete files older than 7 days from ftp://my.ftpsite.net/folder/subfolder
ftpuse F: my.ftpsite.net password /USER:username
timeout /t 5
forfiles -p "F:\folder\subfolder" -s -m *.* -d -7 -c "cmd /C DEL @File /Q"
ftpuse F: /DELETE
The batch file is quite self-explanatory: we map the FTP folder using FTPUSE, then we wait 5 seconds to give the system enough time to get it ready. Last but not least, we use the good old forfiles feature – which we already talked about here – to find and delete the files older than X days (7 in our example).
That’s it for now: happy scripting!