Delete files older than N days with forfiles
Windows' forgotten find -mtime equivalent — selects files by age and runs a command on each one.
01Command
$ forfiles /P "C:\Logs" /S /M *.log /D -30 /C "cmd /c echo @path"02What it does
/D -30 matches files last modified more than 30 days ago; /D -2026-09-01 matches a specific date, and /D +7 means newer than 7 days. /S recurses, /M filters by mask, and @path, @fname, @fdate, @ftime are substituted into the /C command. Forfiles is the standard scheduled-task ingredient for log retention on servers.
03Risk
04Undo
✓No undo needed — this command does not change persistent state.
05Sources
- FORFILES - Windows CMDEvidence: SS64 documents /D -30 as files last modified more than 30 days ago, and shows "cmd /c del @path" as the standard deletion pattern.
- forfiles | Microsoft LearnEvidence: Microsoft documents forfiles /D +/-n to select files with last-modified date older/newer than n days, /S for recursion, and /C to run a command per file with @path variables.
Last verified: 2026-09-17 · Category: Files & Folders
06Related solutions
Save a cleanup profile once, then run it unattended on any schedule.
Diagnose NTFS file system errors and bad sectors on any volume — including the system drive while Windows is running — before committing to a repair.
Robocopy is Windows' built-in robust file copier: use /MIR to keep a destination folder as an exact one-way mirror of a source, resumable and restartable.