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 21:12
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » (Closed) How to delete files older than N days in the DOS command line under WINDOWS DigestI View 27,150 Replies 39
Floor 16 Posted 2005-08-26 20:17 ·  中国 河北 石家庄 联通
铂金会员
★★★★
网络独行侠
Credits 6,962
Posts 2,753
Joined 2003-04-16 00:00
23-year member
UID 1565
Gender Male
From 河北保定
Status Offline
Re Will Sort:

Hehe, the reason I only used for /f is that, to be honest, I never really understood many of the usages of the for command. At least, I never quite understood what for /r meant. But just now I tried it myself, and finally understood it a little, so that command of mine can be changed to:
for /r %dst% %%f in (*.*) do if %%~tf LSS %1 del /f /q %%f && echo delete %%f

Hehe, the reason is just that simple. I'm not as persistent in pursuing technology as brother Wil is. My goal is just to get the job done. Honestly, I sincerely admire brother Wil's batch-writing skills, but I feel I'll never reach that level, and I think brother Wil's batch skills are more than enough, but they really aren't easy for people to understand. As for the batch file in the post above yours, I didn't really understand it until I was replying to this post.
偶只喜欢回答那些标题和描述都很清晰的帖子!
如想解决问题,请认真学习“这个帖子”和“这个帖子”并努力遵守,如果可能,请告诉更多的人!
Floor 17 Posted 2005-08-29 13:40 ·  中国 广东 潮州 电信
中级用户
★★
Credits 240
Posts 53
Joined 2005-01-21 00:00
21-year member
UID 35493
Gender Male
Status Offline
A few days ago I wrote two small programs for under DOS, which just happen to be able to accomplish this task.

The batch file content is like this:
******************************************************
@echo off

rem ============================================
rem The purpose of this batch file is to delete all files in d:\datafile
rem whose last modification time was earlier than 7 days ago (not including 7 days)
rem (including files in all subdirectories)
rem ============================================



rem ============================================
rem Get the date from 7 days ago
rem If today is August 25, then the date 7 days ago is August 18
rem ============================================
GetInfo.COM /d:-7
set theDay=%year%-%mon%-%day%



rem ============================================
rem This searches in D:\datafile中搜索8天前(含8 for files modified 8 days ago (including 8 days)
rem that is, files modified on August 17 and earlier
rem ============================================
attrib/s d:\datafile\*.* -h -r -s
:Loop
dirt.com /d:-%theDay% d:\datafile\*.* /q
if %DirtRet%==Fail goto end
del %FPath%\%FName%
goto Loop

:end


******************************************************



The DirT.com and GetInfo.com in it can be downloaded from http://www.13886.com/soft//,


In DOS under win2000, the del command seems to have a parameter that lets you delete files without user confirmation; you can add that parameter to del %FPath%\%FName%

[ Last edited by zybird on 2006-5-6 at 13:38 ]
Floor 18 Posted 2005-08-29 17:02 ·  中国 河北 石家庄 联通
铂金会员
★★★★
网络独行侠
Credits 6,962
Posts 2,753
Joined 2003-04-16 00:00
23-year member
UID 1565
Gender Male
From 河北保定
Status Offline
These two commands are pretty interesting, and quite useful too. Could you upload them here directly as attachments? That website can't be accessed anymore.
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
zjutsoft +1 2008-07-17 11:02
偶只喜欢回答那些标题和描述都很清晰的帖子!
如想解决问题,请认真学习“这个帖子”和“这个帖子”并努力遵守,如果可能,请告诉更多的人!
Floor 19 Posted 2005-08-29 18:47 ·  中国 广东 潮州 电信
中级用户
★★
Credits 240
Posts 53
Joined 2005-01-21 00:00
21-year member
UID 35493
Gender Male
Status Offline
Originally posted by Climbing at 2005-8-29 05:02 PM:
These two commands are pretty interesting, and quite useful too. Could you upload them here directly as attachments? That website can't be accessed anymore.



On ys168, for some reason, it always becomes impossible to download, so now I've uploaded them here.

If you have any questions or run into any errors during use, please tell me! Thanks!


Getinfo.com and dirt.com can now be downloaded directly from the website below!!

http://www.13886.com/soft/

[ Last edited by zybird on 2006-5-6 at 13:37 ]
Floor 20 Posted 2005-08-29 23:47 ·  中国 河北 石家庄 联通
铂金会员
★★★★
网络独行侠
Credits 6,962
Posts 2,753
Joined 2003-04-16 00:00
23-year member
UID 1565
Gender Male
From 河北保定
Status Offline
Thanks, friend zybird. Were both of these commands developed with Qbasic?
偶只喜欢回答那些标题和描述都很清晰的帖子!
如想解决问题,请认真学习“这个帖子”和“这个帖子”并努力遵守,如果可能,请告诉更多的人!
Floor 21 Posted 2005-08-30 13:10 ·  中国 广东 潮州 电信
中级用户
★★
Credits 240
Posts 53
Joined 2005-01-21 00:00
21-year member
UID 35493
Gender Male
Status Offline
You're welcome!

This was written with tc++ 3.0
Floor 22 Posted 2005-09-02 15:25 ·  中国 山西 太原 中移铁通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
Re All:

Because of personal reasons, I hadn't been on the forum for a week, and I found that quite a lot had changed! Now I am attaching the set /a method mentioned in item 1 of the problem-solving ideas in post 9 for calculating date differences. As for third-party programs, zybird's program is already very good; my respects to brother zybird.

:: DateDel2.bat - delete files modified before the specified number of days under the specified path
:: Will Sort - 2005-9-2 - CMD@WinXP
:: Notes: parameter %1 specifies the number of days; yesterday is 1, the day before yesterday is 2, and so on
:: parameter %1 specifies the file path; if omitted, the current path will be processed
:: Core algorithm: Ritchie Lawrence, updated 2002-08-13. Version 1.1
::
@echo off & setlocal ENABLEEXTENSIONS
call ate2Day %date:~0,10% sdays
set /a sdays-=%1
call ay2Date %sdays% difdate
echo The following files modified before %difdate% under %cd% will be deleted:
for /r %2 %%f in (*.*) do if "%%~tf" LEQ "%difdate%" echo "%%f"
goto :EOF

ate2Day
setlocal ENABLEEXTENSIONS
for /f "tokens=1-3 delims=/-, " %%a in ('echo/%1' do (
set yy=%%a & set mm=%%b & set dd=%%c
)
set /a dd=100%dd%%%100,mm=100%mm%%%100
set /a z=14-mm,z/=12,y=yy+4800-z,m=mm+12*z-3,j=153*m+2
set /a j=j/5+dd+y*365+y/4-y/100+y/400-2472633
endlocal&set %2=%j%&goto :EOF

ay2Date
setlocal ENABLEEXTENSIONS
set /a i=%1,a=i+2472632,b=4*a+3,b/=146097,c=-b*146097,c/=4,c+=a
set /a d=4*c+3,d/=1461,e=-1461*d,e/=4,e+=c,m=5*e+2,m/=153,dd=153*m+2,dd/=5
set /a dd=-dd+e+1,mm=-m/10,mm*=12,mm+=m+3,yy=b*100+d-4800+m/10
(if %mm% LSS 10 set mm=0%mm%)&(if %dd% LSS 10 set dd=0%dd%)
endlocal&set %2=%yy%-%mm%-%dd%&goto :EOF
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 23 Posted 2005-10-17 19:56 ·  中国 福建 三明 电信
中级用户
★★
Credits 316
Posts 83
Joined 2005-03-01 00:00
21-year member
UID 36626
Gender Male
Status Offline
Question for all you experts,
why is it that under the win2000 command line, environment variables like year mon day etc. cannot be displayed?

D:\MULTIT~1\Getinfo>getinfo /d:-2
D:\MULTIT~1\Getinfo>echo %day%
%day%
Floor 24 Posted 2005-10-17 20:10 ·  中国 福建 三明 电信
中级用户
★★
Credits 316
Posts 83
Joined 2005-03-01 00:00
21-year member
UID 36626
Gender Male
Status Offline
Quote:
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!!!



Agreed!!!

[ Last edited by smileseeker on 2005-10-17 at 20:12 ]
Floor 25 Posted 2005-10-19 21:10 ·  中国 广东 潮州 电信
中级用户
★★
Credits 240
Posts 53
Joined 2005-01-21 00:00
21-year member
UID 35493
Gender Male
Status Offline
getinfo.com currently still cannot modify environment variables in cmd.exe; cmd.exe is not the same as dos
Floor 26 Posted 2005-11-10 00:20 ·  中国 浙江 杭州 电信
新手上路
Credits 10
Posts 5
Joined 2005-11-09 22:55
20-year member
UID 44940
Gender Male
From 中国杭州
Status Offline
“for /f "tokens=*" %f in ('dir /b/s') do @if %~tf LSS 2005-08-17 echo %~tf %f”
What does the “%” in there represent? Could someone please explain?
Floor 27 Posted 2007-06-08 09:35 ·  中国 江苏 无锡 电信
初级用户
Credits 80
Posts 43
Joined 2007-04-21 23:44
19-year member
UID 86017
Gender Male
Status Offline
Damn, I can't understand it```` I'm dizzy.
Floor 28 Posted 2007-10-10 10:23 ·  中国 山东 泰安 联通
新手上路
Credits 2
Posts 1
Joined 2007-10-10 09:40
18-year member
UID 99318
Gender Male
Status Offline
The method above works well for deleting files. Can it avoid deleting folders?
Floor 29 Posted 2007-10-10 19:24 ·  中国 北京 鹏博士BGP
新手上路
Credits 12
Posts 4
Joined 2006-10-24 11:19
19-year member
UID 68043
Gender Male
Status Offline
A newbie asking: if the OS regional setting is English and date displays as 10/10/2007 wed, how should the batch file be written??
Floor 30 Posted 2007-10-12 09:54 ·  中国 四川 德阳 联通
初级用户
Credits 51
Posts 24
Joined 2007-09-18 17:11
18-year member
UID 97711
Gender Male
Status Offline
Expert indeed,,, so awesome... learned a lot
Forum Jump: