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?
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:
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:
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.
Related Articles
Quickly Delete Massive File Structure on Windows Server 2012
How to Retrieve Word Document You Forgot to Save on Mac
Is Recuva a Recommendable and Safe Software to Get Deleted Data Back
How to Recover Deleted TIFF Files