China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-06-22 23:01
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » (Closed) How to delete files older than N days in the DOS command line under WINDOWS DigestI View 27,154 Replies 39
Original Poster Posted 2005-08-25 10:47 ·  中国 江苏 南京 电信
初级用户
Credits 25
Posts 6
Joined 2005-08-25 10:44
20-year member
UID 41920
Gender Male
Status Offline
Question: under the DOS command line in WINDOWS, how can I delete certain files older than N days? Thanks!

---------- Edited by willsort ----------
The solutions are as follows (the overall line of attack is in post 9):
1、use set /a to convert between dates and relative day counts, in order to get the target date by difference (post 22);
2、run it every day to keep updating the date list, and use the earliest date at the front of the list as the target difference date (post 11);
3、use a third-party tool to obtain and compare the target difference date (zybird, posts 17 and 19);
In addition, the code for deleting files before a fixed date has also been completed (Climbing, post 13).
---------- Edited by willsort ----------


[ Last edited by willsort on 2005-9-2 at 15:35 ]
Floor 2 Posted 2005-08-25 11:30 ·  中国 江苏 南京 电信
初级用户
Credits 25
Posts 6
Joined 2005-08-25 10:44
20-year member
UID 41920
Gender Male
Status Offline
Does nobody know?
Floor 3 Posted 2005-08-25 11:36 ·  中国 山西 大同 中移铁通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
Re songyujob:

Please tell us which version of Windows you are using. Also, please explain whether "N days ago" refers to some specific dates, or to the previous N days in general?
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 4 Posted 2005-08-25 12:06 ·  中国 河北 石家庄 联通
铂金会员
★★★★
网络独行侠
Credits 6,962
Posts 2,753
Joined 2003-04-16 00:00
23-year member
UID 1565
Gender Male
From 河北保定
Status Offline
Let me ask the OP in return: do you know how to ask a question?

The answer is in my signature.
偶只喜欢回答那些标题和描述都很清晰的帖子!
如想解决问题,请认真学习“这个帖子”和“这个帖子”并努力遵守,如果可能,请告诉更多的人!
Floor 5 Posted 2005-08-25 13:48 ·  中国 江苏 南京 电信
初级用户
Credits 25
Posts 6
Joined 2005-08-25 10:44
20-year member
UID 41920
Gender Male
Status Offline
Originally posted by willsort at 2005-8-25 11:36:
Re songyujob:

Please tell us which version of Windows you are using. Also, please explain whether "N days ago" refers to some specific dates, or to the previous N days in general?

I mean files from the point in time when the command is executed, from the previous N days, for example deleting all files from 8 days ago. I mean 8 days ago in general, not any specific files!
windows 2000 server or windows 2003 server
3KS!!

[ Last edited by songyujob on 2005-8-25 at 13:51 ]
Floor 6 Posted 2005-08-25 13:52 ·  中国 江苏 南京 电信
初级用户
Credits 25
Posts 6
Joined 2005-08-25 10:44
20-year member
UID 41920
Gender Male
Status Offline
Originally posted by Climbing at 2005-8-25 12:06:
Let me ask the OP in return: do you know how to ask a question?

The answer is in my signature.


Sorry! First of all, BS to people like you, talking in such a sarcastic way. Who do you think you are!

[ Last edited by songyujob on 2005-8-25 at 13:53 ]
Floor 7 Posted 2005-08-25 17:19 ·  中国 河北 石家庄 联通
铂金会员
★★★★
网络独行侠
Credits 6,962
Posts 2,753
Joined 2003-04-16 00:00
23-year member
UID 1565
Gender Male
From 河北保定
Status Offline
You've already cursed it out, so what's there to be sorry about. I'm nobody, but at least I know the forum rules far better than you do. Let me ask you in return: who do you think you are?

Speaking sarcastically is a personal style. Tell me, what exactly did I say wrong? Wasn't I trying to help you solve the problem?
偶只喜欢回答那些标题和描述都很清晰的帖子!
如想解决问题,请认真学习“这个帖子”和“这个帖子”并努力遵守,如果可能,请告诉更多的人!
Floor 8 Posted 2005-08-25 17:38 ·  中国 河北 石家庄 联通
铂金会员
★★★★
网络独行侠
Credits 6,962
Posts 2,753
Joined 2003-04-16 00:00
23-year member
UID 1565
Gender Male
From 河北保定
Status Offline
I tried it, and this command is helpful for solving the OP's problem:

for /f "tokens=*" %f in ('dir /b/s') do @if %~tf LSS 2005-08-17 echo %~tf %f
偶只喜欢回答那些标题和描述都很清晰的帖子!
如想解决问题,请认真学习“这个帖子”和“这个帖子”并努力遵守,如果可能,请告诉更多的人!
Floor 9 Posted 2005-08-25 18:47 ·  中国 山西 临汾 中移铁通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
Re songyujob:

First I'll post the problem-solving idea; the actual code may have to wait a few days due to personal reasons.

1. Use date calculation: calculate the valid seven-day date for files that should be kept, then compare each file's date against it; the date calculation can use a specialized third-party program such as date, or it can use set /a, though you may run into some trouble when handling carry-over between years and months.

2. Use date markers: first create markers for the previous 7 dates counting from today; these may be files or environment variables. Then when the program starts, check whether these markers were updated today. If not, create today's marker and delete the oldest marker; if they were updated, skip this step. Then check whether the file date to be processed matches these markers. If it matches, keep it; otherwise, delete it.
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 10 Posted 2005-08-26 10:37 ·  中国 江苏 南京 电信
初级用户
Credits 25
Posts 6
Joined 2005-08-25 10:44
20-year member
UID 41920
Gender Male
Status Offline
Actually this is very easy to do on AIX systems, with find .... -mtime +N, so on a whim I wanted to automate deletion of DB2 archive logs on a WINDOWS server. Looks like I'll have to write a small program for it; doing it with built-in DOS commands is relatively difficult!

[ Last edited by songyujob on 2005-8-26 at 10:39 ]
Floor 11 Posted 2005-08-26 12:06 ·  中国 山西 运城 中移铁通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
Re songyujob:

Code organized according to idea 2; you need to test it thoroughly.

:: DateDel.bat - demo of deleting files modified before 7 days ago (including exactly 7 days ago)
:: Will Sort - 2005-8-26 - CMD@WinXP
:: Note: do not place this batch file in the directory where files need to be selected for deletion
:: This program only demonstrates deletion; if the demo is correct, remove the echo before del for normal execution
:: If the first execution date is not 2005-8-26, then the retained date list in line 4 must be adjusted
:: The program must be run at least once every day so that the date list can be updated automatically
@echo off
if "%1"==":" goto %2
if exist %temp%.\_DateDel.bat %temp%.\_DateDel.bat
%0 : Update 2005-08-20 2005-08-21 2005-08-22 2005-08-23 2005-08-24 2005-08-25 2005-08-26

:Update
setlocal EnableExtensions
set today=%date:~0,10%
if not "%today%"=="%9" %0 : Update %4 %5 %6 %7 %8 %9 %today%
echo %0 : Update %3 %4 %5 %6 %7 %8 %9 >%temp%.\_DateDel.bat

elete
for /r %%f in (*.*) do if "%%~tf" LEQ "%3" echo del "%%f"

:End


[ Last edited by willsort on 2005-8-26 at 19:18 ]
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 12 Posted 2005-08-26 13:51 ·  中国 江苏 南京 电信
初级用户
Credits 25
Posts 6
Joined 2005-08-25 10:44
20-year member
UID 41920
Gender Male
Status Offline
Many thanks! Really, many thanks!
Floor 13 Posted 2005-08-26 18:04 ·  中国 河北 石家庄 联通
铂金会员
★★★★
网络独行侠
Credits 6,962
Posts 2,753
Joined 2003-04-16 00:00
23-year member
UID 1565
Gender Male
From 河北保定
Status Offline
Re willsort:

Does your batch file need the retained date list adjusted before every run? If so, that's too troublesome; it's not as concise as the command I posted earlier.


:: DelOldFile.cmd - demo of deleting all files before a specified date
:: Climbing - 2005-8-26 - CMD@WinXP
@echo off
if "%1"=="" goto _usage
set dst=.\.
if not "%2"=="" if exist %2\. (set dst=%2\.) else (goto _err1)
for /f "tokens=*" %%f in ('dir /b/s %dst%') do if %%~tf LSS %1 del /f /q %%f && echo delete %%f
goto _quit

:_usage
echo DelOldFile.cmd - delete all files before the specified date in the specified directory
echo Version 1.0 Copyleft by Climbing
echo Syntax: DelOldFile ^<date^>
echo ^<date^>   required parameter, date format: %date%, files before this date will be deleted (not including this date)
echo   optional parameter, specifies the directory whose files are to be deleted; defaults to the current directory
goto _quit

:_err1
echo Error: the specified directory %2 does not exist!

:_quit
set dst=
:End


Example: suppose today is August 26, and I want to delete files from 2 days earlier, that is, delete files before August 23, with the target directory d:\arch. Then the usage is:
DelOldFile 2005-08-23 d:\arch

Tested successfully under cmd in Windows 2003.

Postscript:
After a reminder from brother WillSort, the for /f line in the batch file above can be changed to:

for /r %dst% %%f in (*.*) do if %%~tf LSS %1 del /f /q %%f && echo delete %%f

The effect is the same.

[ Last edited by Climbing on 2005-8-26 at 21:38 ]
偶只喜欢回答那些标题和描述都很清晰的帖子!
如想解决问题,请认真学习“这个帖子”和“这个帖子”并努力遵守,如果可能,请告诉更多的人!
Floor 14 Posted 2005-08-26 19:00 ·  中国 广东 广州 白云区 电信
金牌会员
★★★★
D◎$ Fαп
Credits 4,562
Posts 1,883
Joined 2004-01-19 00:00
22-year member
UID 15812
Gender Male
From 广东广州
Status Offline
Originally posted by Climbing at 2005-8-25 17:38:
for /f "tokens=*" %f in ('dir /b/s') do @if %~tf LSS 2005-08-17 echo %~tf %f


Awesome!!!
----====≡≡≡≡ 我的至爱,永远是MSDOS!≡≡≡≡====----
Floor 15 Posted 2005-08-26 19:17 ·  中国 山西 临汾 中移铁通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
Re Climbing:

When the program in post 11 is run for the first time, I need to adjust the date list in the code. After that, it needs to be run once a day to automatically update the date list in %TEMP%.\_DateDel.bat; the code in the :Update section is used to accomplish this task. So it is suitable for a batch program that runs daily.

Not long after posting, I noticed that I had forgotten to include the daily run requirement in the notes, but I was busy exploring a way to implement idea 1 so I could post it together, so I kept putting off the correction. Now I've added the new note to the original code.

As for the code you wrote, I still haven't understood why you used for /f "tokens=*" rather than for /r. Can you explain?

[ Last edited by willsort on 2005-8-26 at 19:33 ]
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Forum Jump: