habibzain Just husband, father and enthusiastic men about System Administration. Love to write short article about it. Perhaps can help and be useful for others.

How To Delete Files Older Than X Days in Windows Use CMD

59 sec read

How-To-Delete-Files-Older-Than-X-Days-in-Windows-Use-CMD

Hi Guys. I will share how to delete files in the x days folder ago. this will be boring if done by humans. So, we can delegate this repeat job to a computer machine. Lets practice delete older file windows.
This will be useful when we will delete the daily backup file.

I use CMD Command Prompt and run the FORFILES command.

FORFILES /p "D:\DB-BACKUP" /m *.* /c "cmd /c Del /F /Q @path" /d -14 /

The parameters we can use are as follows:

/S - This switch makes forfiles recurse subdirectories. Like "DIR /S".
/D - Select files with a last modified date. For example,-365 means over a year ago, -30 means a month ago.
/P  - To indicate the path to start the search.
/C "command" - This command specifies the command to execute on each file that will be found. Command strings should be wrapped in double quotes. 
Delete file folder x day
Delete file folder x day

The default command is “cmd /c echo @file”.

The following variables can be used in the command string:

  • @file – returns the name of the file.
  • @fname – returns the file name without extension.
  • @ext – returns only the extension of the file.
  • @path – returns the full path of the file.
  • @relpath – returns the relative path of the file.
  • @isdir – returns “TRUE” if a file type is a directory, and “FALSE” for files.
  • @fsize – returns the size of the file in bytes.
  • @fdate – returns the last modified date of the file.
  • @ftime – returns the last modified time of the file.

This is simple, you can modified this command what’s you need. like scheduler.

See also  How to Add Static IP Route on Windows
habibzain Just husband, father and enthusiastic men about System Administration. Love to write short article about it. Perhaps can help and be useful for others.

Leave a Reply

Your email address will not be published. Required fields are marked *

Never miss good article from us, get weekly updates in your inbox