How to Create A Batch File to Delete Files Older Than N Days

Hello guys. I need a batch file to delete files in a folder older than N days. I could run it, but the job fails when there are no files. How to ignore/bypass when there are no files. My scripts fail if no files exist at that location. What should I do?

Best Answered by

Dany· Answered on Jul 20, 2023

To clear up space and keep your files organized, you can use Command Prompt and Task Scheduler in Windows to automatically remove files older than a specified number of days.

However, please ensure that all of the files to be evaluated for deletion are in a parent folder. It's OK if this parent folder has subfolders because the batch script will also search for and remove files in subfolders.

To begin, launch the Command Prompt with administrative privileges. To do so, type cmd into the Taskbar search box, select Run as administrator and then press the Yes button. You can use this command in a Batch file:

  • forfiles /p "PATH" /s /d -15 /c "cmd /c del @file : date >= 15 days >NUL"

Change "PATH" to the unique path you run the command for. This example uses 15 days as the required age, but you can adjust this by changing both numbers in bold.

The command breakdown for the CMD line is as follows:

  • /p - specifies the pathname to look for.
  • /s - ForFiles to search within subdirectories.
  • /d - specifies a file's last modified date.
  • /c - tells ForFiles to run the command (must be wrapped in double quotes). "cmd /c del @file" is the default."
  • /q - Allows you to delete directories without asking for confirmation.

If any of your files become corrupted while the script is running, you can learn how to delete corrupted files using a variety of approaches. However, if you enter the wrong path and accidentally delete some important files, you should immediately stop using your hard drive and utilize a file recovery application to recover them. EaseUS Data Recovery Wizard is a powerful data recovery software that can recover deleted, lost, formatted, inaccessible or corrupted files.

Use EaseUS Data Recovery Wizard to recover deleted or lost files easily and quickly.

Tutorials on how to recover lost, deleted, or formatted data from HDD, SSD, USB, SD card, or other storage media on Windows and Mac.

People Also Ask

How to Delete Files in File Server Automatically

Are you tired of useless files taking up space on your computer? Then, using Linux, and CMD, utilize this straightforward method to delete files older than x days. We can also help you restore inadvertently deleted files on Windows 11/10.

Dany

Jul 20, 2023

How to Delete Files Older Than 90 Days and Save A Copy

You may free up space and keep things organized by only deleting files in any folder that is older than a specific number of days. If you have many files in separate directories and want to clear them up by eliminating those older than a particular number of days, you can use the ForFiles command.

Dany

Jul 20, 2023

How to Recover Deleted Books from Kindle [2023 Guideline]

Have you accidentally deleted the book from your Kindle account? Download the free and robust Kindle recovery tool when you find no other way to restore deleted texts, images, pictures, or videos from Kindle Fire HD or Kindle Paperwhite.

Dany

Jul 20, 2023

Recover Accidentally Deleted Excel Files with Software

Accidentally delete Excel files? Don't worry! This article will introduce a powerful Excel file recovery software that can easily recover accidentally deleted Excel files. Read on to get detailed information.

Brithny

Jul 20, 2023

How to Recover Deleted Minecraft File on PS4

Did you lose game data on your PC, Steam, PS4, or other devices? Don't panic. You can find the best ways to recover lost game data from any device. EaseUS game data recovery software best retrieves lost game data from PS4. Read on to get more information.

Dany

Jul 20, 2023

Script to Delete Files Older Than 7 Days

You can move files older than 7 days to another folder with a script or using command lines. Let us find out how to move the files instead of deletion.

Dany

Jul 20, 2023