1.

Solve : Delete by date modified?

Answer»

What is the command to delete files by date modified?

Specifically, I want to delete all files in a folder with a modified date older than 5 days. TIAC:\test>type  mdel.bat
echo off


forfiles /s /m *.bat /d -5 /c "cmd /c dir  /tw  file" |  findstr  ".bat" 
rem  forfiles  /?

OUTPUT:

C:\test> mdel.bat

.
.
.



10/11/2010  03:34 PM               171 shiftarg.bat
10/11/2010  10:19 PM               161 bc.bat
10/12/2010  02:01 PM               269 lori.bat
10/13/2010  07:30 AM               343 not.bat
10/13/2010  07:41 AM               292 not2.bat
10/13/2010  09:36 AM               405 not3.bat
10/21/2010  03:20 PM                87 datetime.bat
10/26/2010  03:29 PM                85 st1026.bat
10/26/2010  03:49 PM                50 replace.bat
10/28/2010  06:58 PM                99 st102810.bat
10/28/2010  08:06 PM               135 down.bat
10/29/2010  08:21 AM               653 st1029.bat
10/31/2010  09:23 PM                 0 cpfld.bat
11/03/2010  09:56 PM               161 bccmp.bat
11/04/2010  11:54 AM                75 dosout.bat
11/05/2010  05:52 PM               255 ken11510.bat
11/07/2010  05:12 PM               198 larry11710.bat
11/07/2010  06:33 PM               163 cntbytes.bat
11/07/2010  07:41 PM               263 cntbites.bat
11/09/2010  06:31 PM               280 wh.bat
11/11/2010  09:50 PM               340 thirydays.bat
11/11/2010  10:13 PM               414 sam.bat
11/12/2010  05:58 PM               370 paul.bat
11/13/2010  10:47 AM               308 mp.bat
11/14/2010  06:25 AM                29 deuce.bat
11/15/2010  09:05 AM               265 err.bat
11/16/2010  02:24 PM               146 slim.bat
11/16/2010  03:49 PM               277 thin.bat
11/16/2010  07:11 PM               315 adh.bat
11/16/2010  07:48 PM                31 pair1.bat
11/16/2010  07:49 PM                31 pair2.bat
11/16/2010  08:12 PM               217 killnote.bat
11/16/2010  10:29 AM               154 file_or_Dir.bat
11/17/2010  03:52 PM               189 greg.bat
11/18/2010  01:47 PM                 0 skr.bat
11/18/2010  02:11 PM               510 slow.bat
11/18/2010  09:27 PM               311 prem.bat
11/19/2010  01:41 PM               223 adhold.bat
11/19/2010  02:11 PM               328 killtwo.bat
11/19/2010  02:40 PM               328 killpair.bat
11/19/2010  09:07 PM               130 jp.bat
11/19/2010  10:19 AM             2,217 sidew.bat
11/19/2010  10:22 AM             1,748 sw1119110.bat
11/20/2010  05:03 PM               503 dusty.bat
11/20/2010  05:53 PM                36 ser.bat
11/20/2010  08:19 PM               188 killit.bat
11/23/2010  06:26 PM             2,913 yarosav.bat
11/23/2010  06:44 PM             1,826 yaro.bat
11/24/2010  03:21 AM               209 greater.bat
11/24/2010  03:49 AM               235 len.bat
11/24/2010  05:06 PM               508 dusty2.bat
11/24/2010  08:09 PM               487 shan.bat
11/24/2010  08:13 PM               433 len2.bat
11/25/2010  02:17 PM                 0 tab.bat
11/25/2010  02:29 AM               629 swlen.bat
11/25/2010  03:50 PM               134 starttitle.bat
11/25/2010  04:03 PM               180 killtitle.bat
11/25/2010  04:06 PM               151 killme.bat
11/25/2010  04:59 PM               857 killyahoo.bat
11/26/2010  01:00 PM               196 whatever.bat
11/26/2010  03:40 PM               215 yar.bat
11/26/2010  12:08 PM               405 killchrome.bat
11/27/2010  03:45 PM                60 test2.bat
11/27/2010  03:46 PM                58 test3.bat
11/27/2010  04:12 PM               381 backg.bat
11/29/2010  07:25 PM               421 conif.bat
11/30/2010  07:45 PM               134 dave.bat
11/30/2010  08:18 PM               243 dave2.bat
12/01/2010  09:27 PM               298 ran2.bat
12/02/2010  07:47 PM               591 st120210.bat
12/02/2010  07:57 PM               662 ran20.bat
12/03/2010  03:30 PM               707 stran12310.bat
12/04/2010  10:43 AM               577 ransw.bat
12/04/2010  11:13 AM               516 ran2sw.bat
12/04/2010  11:50 AM                 0 sorit.bat
12/05/2010  07:19 PM               320 swrdn5.bat
12/05/2010  08:28 PM               399 brand.bat
12/05/2010  11:57 PM               777 brian.bat
12/06/2010  05:46 PM               734 realmenu.bat
12/07/2010  04:39 PM                46 cleanup.bat
12/07/2010  04:43 PM               309 bb.bat
12/10/2010  07:34 PM                96 bobt.bat
12/10/2010  10:44 PM                20 crest.bat
12/11/2010  06:57 PM               480 brand2.bat
12/12/2010  08:33 AM               413 chat.bat
12/12/2010  11:12 PM               998 vox2.bat
12/12/2010  11:39 PM                89 vox.bat

C:\test>Reply #1  will find the files older than five days.

redirect the output to a text file and delete all files older than five days  with a for loop. Quote from: donald99 on December 18, 2010, 10:57:42 PM

Reply #1  will find the files older than five days.

redirect the output to a text file and delete all files older than five days  with a for loop.

Why not just tell forfiles to delete the files older than 5 days? I know you like redirecting, Bill, but it is not necessary in this case.

I suspect Bill just found out about forfiles and logged in as a DIFFERENT user to ask a "question" that he could proceed to "answer" as donald99, who therefore won't complain that his question was not answered properly.

The original question asked how to delete all files in a folder older than 5 days. Bill showed how to use forfiles to make a file containing a list of .bat files in a folder and all subfolders under it, that were older than 4 days. He then issued a vague suggestion to "use a for loop" to delete those files. Ah the "for loop" - forever beyond Bill's comprehension.

However it may be that somebody sometime might really want to know how to use the forfiles command to delete files older than a certain age (Google please index here!). This can be done all in one line so a script may not even be necessary.

Let's build the command...

1. We type forfiles and a space (which you can't see yet, but it's there. Trust me.)

FORFILES

2. Next we add the /M switch with a file mask to show what files to delete - "all files" was asked for so we use *.* and another space.

FORFILES /M *.*

3. Next we add the date switch /D with a space and then a negative number and then another space. This number tells forfiles to select files with a modified date less than or equal to today's date minus that number of days. This means that if you put /D -5 you will select all those files which were modified more than 4 days ago. So take care here. Since the OP asked how to delete files "with a modified date older than 5 days", the correct number would be -6.

FORFILES /M *.* /D -6

4. Now we add the /C switch and a space to tell forfiles that what is coming next is a command string.

FORFILES /M *.* /D -6 /C

5. At last we can specify the command which we want to perform on these selected files. To delete the file we specify CMD with the /C switch, followed by a space then DEL and a space and finally FILE and we put the whole lot in double quotes.

FORFILES /M *.* /D -6 /C "CMD /C DEL FILE"

There you are. Finished. Although I have used upper case in fact you can use lower case, or a mixture.







Quote from: Salmon Trout on December 19, 2010, 01:52:11 AM
Why not just tell forfiles to delete the files older than 5 days? I know you like redirecting but it is not necessary in this case.

I was unable to test the delete function of forfiles. But it should work.

Thanks for the suggestion

p.s The for loop is an option. Quote from: donald99 on December 19, 2010, 04:13:46 AM
I was unable to test the delete function of forfiles. But it should work.

Thanks for the suggestion

p.s The for loop is an option.

Oh right! It should work. That is reassuring. Did Bill Gates tell you that himself?

You don't mind me calling you Bill, do you?


Quote from: Salmon Trout on December 19, 2010, 04:14:47 AM

Salmon Trout,

Please show us the CODE and an output  sample for the "delete by date modified" forfiles code.

Thanks for your help.

p.s.  forfiles /? Quote from: donald99 on December 19, 2010, 04:21:09 AM
Salmon Trout,

Please show us the code and an output  sample for the "delete by date modified" forfiles code.

Thanks for your help.

p.s.  forfiles /?

I already showed the one line of code. There is no "output". Maybe time to lock this thread, prior to banning Bill again?


Quote from: Salmon Trout on December 19, 2010, 04:07:57 AM
The original question asked how to delete all files in a folder older than 5 days.

However it may be that somebody sometime might really want to know how to use the forfiles command to delete files older than a certain age (Google please index here!). This can be done all in one line so a script may not even be necessary.

Let's build the command...

1. We type forfiles and a space (which you can't see yet, but it's there. Trust me.)

FORFILES

2. Next we add the /M switch with a file mask to show what files to delete - "all files" was asked for so we use *.* and another space.

FORFILES /M *.*

3. Next we add the date switch /D with a space and then a negative number and then another space. This number tells forfiles to select files with a modified date less than or equal to today's date minus that number of days. This means that if you put /D -5 you will select all those files which were modified more than 4 days ago. So take care here. Since the OP asked how to delete files "with a modified date older than 5 days", the correct number would be -6.

FORFILES /M *.* /D -6

4. Now we add the /C switch and a space to tell forfiles that what is coming next is a command string.

FORFILES /M *.* /D -6 /C

5. At last we can specify the command which we want to perform on these selected files. To delete the file we specify CMD with the /C switch, followed by a space then DEL and a space and finally FILE and we put the whole lot in double quotes.

FORFILES /M *.* /D -6 /C "CMD /C DEL FILE"

There you are. Finished. Although I have used upper case in fact you can use lower case, or a mixture.

Salmon Trout,

Where is a sample of FORFILES /M *.* /D -6 /C "CMD /C DEL FILE"
output? Quote from: donald99 on December 19, 2010, 04:33:00 AM
Salmon Trout,

Where is a sample of FORFILES /M *.* /D -6 /C "CMD /C DEL FILE"
output?

There isn't any. The "output" is the deletion of the files. Please quit trolling.
ST  wrote:

"There isn't any. The "output" is the deletion of the files."


Maybe a notice from the OS that the files were deleted successfully?

p.s.  OS means Operating System Quote from: donald99 on December 19, 2010, 04:51:41 AM

Maybe a notice from the OS that the files were deleted successly?

p.s.  OS means Operating System

Well, I leave that as an exercise for the reader. In any case deletion can be verified by inspection. Or by running the command a second time and verifying that the message "ERROR: No files found with the specified search CRITERIA." is seen. Or by doing DIR before and after.

Successly? What language is that?

What part of "please quit trolling" did you fail to comprehend?




You wanted output...

Code: [Select]S:\Test\forfiles-del>forfiles /M *.* /D -6 /C "cmd /c echo delete fdate file & del file"

delete 07/12/2010 "TestFile001.zip"
delete 07/12/2010 "TestFile002.zip"
delete 08/12/2010 "TestFile003.rar"
delete 10/12/2010 "TestFile005.exe"
delete 03/12/2010 "TestFile006.PDF"
delete 06/12/2010 "TestFile007.nzb"
delete 06/12/2010 "TestFile008.nzb"
delete 13/12/2010 "TestFile009.nzb"
delete 07/12/2010 "TestFile010.zip"
delete 11/12/2010 "TestFile011.nzb"
delete 05/12/2010 "TestFile012.nzb"
delete 10/12/2010 "TestFile013.nzb"
delete 03/12/2010 "TestFile016.PDF"
delete 03/12/2010 "TestFile017.PDF"
delete 10/12/2010 "TestFile019.zip"
delete 01/12/2010 "TestFile020.zip"
delete 11/12/2010 "TestFile024.zip"
delete 05/12/2010 "TestFile025.zip"
delete 01/12/2010 "TestFile027.zip"
delete 05/12/2010 "TestFile028.exe"
delete 11/12/2010 "TestFile029.exe"

S:\Test\forfiles-del>forfiles /M *.* /D -6 /C "cmd /c echo delete fdate file & del file"
ERROR: No files found with the specified search criteria.

S:\Test\forfiles-del>

Define Troll:

"Online false statement used as Internet lure: a carefully worded but incorrect statement that is designed to lure other Internet users into sending responses."

http://www.bing.com/Dictionary/search?q=define+troll&FORM=DTPDIA&qpvt=define+troll

p.s.  I cannot find any posts by Donald99  that fits the above definition


Discussion

No Comment Found