How to delete files older than X days from FTP server or folder with a batch file in Windows

How to map any FTP server and/or folder to a Windows drive letter using FTPUSE

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:

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!



About Ryan

IT Project Manager, Web Interface Architect and Lead Developer for many high-traffic web sites & services hosted in Italy and Europe. Since 2010 it's also a lead designer for many App and games for Android, iOS and Windows Phone mobile devices for a number of italian companies.

View all posts by Ryan