Deleting Files datewise

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
sajjansinghania
Posts: 18
Joined: 23 Apr 2017 22:36

Deleting Files datewise

#1 Post by sajjansinghania » 24 May 2017 14:15

I have Windows 8.1 & Firefox Latest.
I want to create a Cmd file, which can delete all files in target folders/sub folders earlier than 15 days. This Cmd file is needed to run every day.
https://drive.google.com/file/d/0B1calHz69fSuRzNKb214ajkxM2c/view?ts=5925e6a9
Is it possible ? IF yes, Kindly post sample Cmd Script.
Thanks in advance.

ShadowThief
Expert
Posts: 1160
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Deleting Files datewise

#2 Post by ShadowThief » 24 May 2017 15:47

Earlier than 15 days? Today is the 24th, fifteen days ago was the 9th. If I ran the script today, would you expect files from the 8th to be deleted, or would you expect files from the 10th to be deleted?

sajjansinghania
Posts: 18
Joined: 23 Apr 2017 22:36

Re: Deleting Files datewise

#3 Post by sajjansinghania » 24 May 2017 23:39

I was meaning older than 15 days on the date of run. In other words, today I have files from Jan 1, 2017. If I run Cmd file today, files 15 older be deleted as on today. When I run same Cmd file on June1, 2017, files older than that date be deleted. I hope I clarified.
Thank you in advance, for responding ShadowThief Sir.

elzooilogico
Posts: 128
Joined: 23 May 2016 15:39
Location: Spain

Re: Deleting Files datewise

#4 Post by elzooilogico » 25 May 2017 03:56

This will list files

Code: Select all

forfiles -p "C:\what\ever\dir" -s -m *.* -d -15 -c "cmd /c echo @path"

once you know its ok

Code: Select all

forfiles -p "C:\what\ever\dir" -s -m *.* -d -15 -c "cmd /c del @path"

To run every day, create a task

Code: Select all

schtasks /create /SC DAILY /ST "04:00" /TN "your-task-name" /RL HIGHEST /TR "your-script-path-plus-name" /F

sajjansinghania
Posts: 18
Joined: 23 Apr 2017 22:36

Re: Deleting Files datewise

#5 Post by sajjansinghania » 25 May 2017 04:44

elzooilogico Sir,
I am going to try all that you explained and it looks lovely. Pleas approve then I shall run these files.
1. Created D:\Cmd\List.Cmd

Code: Select all

forfiles -p "D:\Instal" -s -m *.* -d -15 -c "cmd /c echo @E:\Instal"
rem forfiles -p "C:\what\ever\dir" -s -m *.* -d -15 -c "cmd /c echo @path"

2.Created D:\Cmd\Confirm.Cmd

Code: Select all

forfiles -p "D:\Instal" -s -m *.* -d -15 -c "cmd /c echo @E:\Instal"
rem forfiles -p "C:\what\ever\dir" -s -m *.* -d -15 -c "cmd /c echo @path"

3.Created D:\Cmd\Schedule.Cmd

Code: Select all

schtasks /create /SC DAILY /ST "10:00" /TN "Delete&update" /RL HIGHEST /TR "D:\Cmd\Delete.Cmd" /F
rem schtasks /create /SC DAILY /ST "04:00" /TN "your-task-name" /RL HIGHEST /TR "your-script-path-plus-name" /F

One more Sir, do I need to run D:\Cmd\Schedule.Cmd just once or daily.
Kindly confirm or suggest edit.
Thanks tons Sir.
I shall be ever grateful to you Sir.

elzooilogico
Posts: 128
Joined: 23 May 2016 15:39
Location: Spain

Re: Deleting Files datewise

#6 Post by elzooilogico » 25 May 2017 05:09

your file (i.e D:\cmd\deleteTask.cmd) should look like

Code: Select all

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

@path is a `forfiles` special word that tells the command to use the full path\filename of all files matching the condition given.

see forfiles /? to get help on switches.

You don`t need more batch files.

Schtask is the command to create an scheduled task, you can run it in the command line.

So, with the batch file newly created simply execute this in the command line

Code: Select all

schtasks /create /SC DAILY /ST "10:00" /TN "Delete&update" /RL HIGHEST /TR "D:\cmd\deleteTask.cmd" /F


EDIT and WARNING
But before doing this, I suggest you run the following in command line to test if the condition grabs exactly what you want.

Code: Select all

forfiles -p "D:\Instal" -s -m *.* -d -15 -c "cmd /c echo @path @fdate"

Post Reply