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.
- Rename Part of Filename in Batch Script
- Run Batch File Remotely
- Transfer File to Raspberry Pi Using Batch Script
- Run Multiple Files in Batch Script
- New Line in Batch Script
- Use Batch File to Write to a Text File