![]() |
China DOS Union-- Unite DOS · Advance DOS · Grow DOS --Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum |
| Guest | Log in | Register | Members | Search | China DOS Union |
|
中国DOS联盟论坛 The time now is 2026-08-07 20:38 |
48,041 topics / 350,128 posts / today 3 new / 48,252 members |
| DOS批处理 & 脚本技术(批处理室) » (Closed) How to delete files older than N days in the DOS command line under WINDOWS |
| Printable Version 28,324 / 39 |
| Floor1 songyujob | Posted 2005-08-25 10:47 |
| 初级用户 Posts 6 Credits 25 | |
|
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 ] |
|
| Floor2 songyujob | Posted 2005-08-25 11:30 |
| 初级用户 Posts 6 Credits 25 | |
|
Does nobody know?
|
|
| Floor3 willsort | Posted 2005-08-25 11:36 |
| 元老会员 Posts 1,512 Credits 4,432 | |
|
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? |
|
| Floor4 Climbing | Posted 2005-08-25 12:06 |
| 铂金会员 Posts 2,753 Credits 6,962 From 河北保定 | |
|
Let me ask the OP in return: do you know how to ask a question?
The answer is in my signature. |
|
| Floor5 songyujob | Posted 2005-08-25 13:48 |
| 初级用户 Posts 6 Credits 25 | |
Originally posted by willsort at 2005-8-25 11:36: 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 ] |
|
| Floor6 songyujob | Posted 2005-08-25 13:52 |
| 初级用户 Posts 6 Credits 25 | |
Originally posted by Climbing at 2005-8-25 12:06: 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 ] |
|
| Floor7 Climbing | Posted 2005-08-25 17:19 |
| 铂金会员 Posts 2,753 Credits 6,962 From 河北保定 | |
|
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? |
|
| Floor8 Climbing | Posted 2005-08-25 17:38 |
| 铂金会员 Posts 2,753 Credits 6,962 From 河北保定 | |
|
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 |
|
| Floor9 willsort | Posted 2005-08-25 18:47 |
| 元老会员 Posts 1,512 Credits 4,432 | |
|
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. |
|
| Floor10 songyujob | Posted 2005-08-26 10:37 |
| 初级用户 Posts 6 Credits 25 | |
|
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 ] |
|
| Floor11 willsort | Posted 2005-08-26 12:06 |
| 元老会员 Posts 1,512 Credits 4,432 | |
|
Re songyujob:
Code organized according to idea 2; you need to test it thoroughly. [ Last edited by willsort on 2005-8-26 at 19:18 ] |
|
| Floor12 songyujob | Posted 2005-08-26 13:51 |
| 初级用户 Posts 6 Credits 25 | |
|
Many thanks! Really, many thanks!
|
|
| Floor13 Climbing | Posted 2005-08-26 18:04 |
| 铂金会员 Posts 2,753 Credits 6,962 From 河北保定 | |
|
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. 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: The effect is the same. [ Last edited by Climbing on 2005-8-26 at 21:38 ] |
|
| Floor14 JonePeng | Posted 2005-08-26 19:00 |
| 金牌会员 Posts 1,883 Credits 4,562 From 广东广州 | |
Originally posted by Climbing at 2005-8-25 17:38: Awesome!!! |
|
| Floor15 willsort | Posted 2005-08-26 19:17 |
| 元老会员 Posts 1,512 Credits 4,432 | |
|
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 ] |
|
| 1 2 3 Next |
|
[ Contact the Union admin team -
中国DOS联盟 -
Standard version ] Sponsored by ifanr Inc | © 2001–2023 |