How to Delete Files Older Than N Days Using Batch Script
In this article, we will delete a file that is N days old using Batch Script.
Delete Files Older Than N Days Using Batch Script
The general format of code to perform this task is shown below.
FORFILES /p "D:\DIRECTORY" /S /M *.* /D - /C "CMD /C DEL @path"
Here is something we need to understand about this code.
/s
means subfolders of that directory. /m *.*
specifies all the file formats on that directory to delete.
/D
is for a date. /C
provides the actions for each file, which is cmd /c del @path
in our case.
Let’s see an example. The below code will delete the file in a directory that is 6 days old.
@echo off
FORFILES /p "D:\DIRECTORY" /s /m *.* /D -6 /C "cmd /c del @path"
All the method we discussed above is only for Windows CMD.
Aminul Is an Expert Technical Writer and Full-Stack Developer. He has hands-on working experience on numerous Developer Platforms and SAAS startups. He is highly skilled in numerous Programming languages and Frameworks. He can write professional technical articles like Reviews, Programming, Documentation, SOP, User manual, Whitepaper, etc.
LinkedIn