CoolComputing Logo
Coupons/Deals  ·  New Promo Codes/Coupons  · August 18, 2018

Automatically Delete Old Files in a Folder in Windows

Posted on Wednesday, September 18, 2013 @ 03:50:56 PM CDT by David Yee [] [read 6215 times]

Tips: Windows-world
I have a Foscam FI8910W camera that we setup as a baby monitor. I have the IP Cam automatically upload captured images whenever there is motion, via FTP, to my Windows 8 PC running Filezilla FTP Server. It's a nice setup, except I end up with tens of thousands of old screen .jpg captures in my ftp folder (which I have located at "D:\foscam") that I have to manually delete. So I wonder, can I have Windows automatically delete files in this folder that are older than, let's say 3 days? This is because even though I'm running a beefy i7-3770 PC, going from one photo to the next was slow because there were so many files in one directory. I did put together a solution, and here is how I did it.

First I had to figure out a command that will delete only files in the "D:\foscam" folder that are older than certain days. This was that magical command that would do it.

forfiles /p "full_path_of_folder" /s /m *.* /d -number_of_days /c "cmd /c del @path"

The command forfiles basically is the file finder of sorts, giving you a filtered list of files that matches the search criteria you provide. In this case "/d" is the parameter that filters for files that match the last modified time from the current date. The "/c" pipes, or sends the list of the matching files to the next command, which is "del" to delete. You can read more info on the forefiles command.

So for my purpose, the final command would be:

forfiles /p "D:\foscam" /s /m *.* /d -3 /c "cmd /c del @path"

The days is "-3" because I'm looking for files that are older than 3 days. Before continuing, I wanted to test to make sure the command works. So I copied a few old files over to a folder called test, and ran the forfiles command without sending to delete. I put 3 old (more than a week old) files + 2 new files from today. And when I ran just the forefiles command, it looked good. This was the output:

Then I added the /c "cmd /c/ del @path" and the 3 matching files deleted without touching the 2 new files from today. The command works. For your purpose, I would recommend you run a test like this before proceeding, especially since the delete will bypass the recycle bin, making it more difficult to recover files that you did not intend to delete.

I then saved my command, calling it "foscam-delete-files.bat" under "C:". If you have trouble saving there, you can save elsewhere, or you can run Notepad as Administrator (right click on Notepad and you will see the option).

Being used to running scheduled tasks via cron in Linux, I was not as familiar with the Windows equivalent in Task Scheduler. But it turned out to be fairly easy to use and setup. Type in "Task Scheduler" in Search, or go to Control Panel -> Administrative Tools to find it, and run it.

On the right side, under Task Schedule Library, I selected "Create Basic Task".

Clicked Next, then for Trigger, I picked Daily, then start time I picked 3 PM, Recur every 1 day. For Action, I selected "Start a program", then browsed for my batch file:

On the confirmation page click finish, and it's all ready to go. If for some reason it does not work for you, try double-clicking on the batch file to run it manually, just to make sure it works. If it runs fine, then check the Task Scheduler entry to make sure it looks okay and that status says "Ready".

Please rate this article or post a brief review of Automatically Delete Old Files in a Folder in Windows or comment on benchmark/performance, features, availability, price or anything else regarding Automatically Delete Old Files in a Folder in Windows. Thanks!

Automatically Delete Old Files In A Folder In Windows Rating: 2.4/5 (45 votes cast)

Your Name:
I have read and agreed to the Review Posting Agreement.
Review Title: