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-21 10:14
中国DOS联盟论坛 » DOS学习入门 & 精彩文章 (教学室) » How to delete directories and files with a specified date and time?? (Looking for improvements from everyone) View 2,365 Replies 4
Original Poster Posted 2004-02-23 00:00 ·  中国 江苏 扬州 联通
铂金会员
★★★★
C++启程者
Credits 5,154
Posts 1,827
Joined 2003-07-18 00:00
22-year member
UID 7105
Gender Male
Status Offline
Using a batch file with the /f /r switches of the XP/2000 for command (requiring only a single statement to process it once), you can delete directories and files with a specified date and time (generally, using the creation time as the basis is more practical; if you want to use the modification time instead, remove the /t:c switch) (including long filenames with spaces).

1-A
The following deletes directories or files created on February 23, 2004 in the root directory of E
@echo off
dir e: /t:c>e:\finddate.txt
for /f "tokens=1,4* delims= " %%1 in (e:\finddate.txt) do if %%1==2004-02-23 deltree /y e:\"%%2*"

1-B
The following deletes files created on February 23, 2004 in directory E, all subdirectories, and the files in them, as follows:
@echo off
dir e: /t:c /s>e:\finddate.txt
for /f "tokens=1,4* delims= " %%1 in (e:\finddate.txt) do for /r e: %%a in ("%%2*" do if %%1==2004-02-23 deltree %%a

(Note! When the program window shows you the directory or filename to be deleted and asks for confirmation, you must look carefully at its exact path and make sure it is the one you want to delete. Otherwise, another directory or file with the same name but a different file time may also be deleted!
If you are sure it can be deleted, or there is no other directory or file with the same name but a different file time, then you can add the /y parameter after deltree.
If you absolutely want to delete only directories or files with the specified time, it gets a bit complicated. It would be better to do it in several steps, but in the end you would still have to find them one by one.
Although the above batch file also requires you to confirm them one by one, it only takes a single statement.
If anyone can use a single statement of about the same length to delete only directories or files with the specified time, hurry up and post it!
I really hope Microsoft or someone will design and add a time parameter like the one in xcopy. That would make it much more convenient!)

*******************************************
If you want it accurate down to the hour and minute,

2-A
To delete directories or files created at 15:31 on February 23, 2004 in the root directory of E, as follows:
@echo off
dir e: /t:c>e:\findtime.txt
for /f "tokens=1,2,4* delims= " %%1 in (e:\findtime.txt) do if %%1%%2==2004-02-2315:31 deltree /y e:\"%%3*"

2-B
If you want to delete files created at 15:31 on February 23, 2004 in directory E, all subdirectories, and the files in them,
as follows:
@echo off
dir e: /t:c>e:\findtime.txt
for /f "tokens=1,2,4* delims= " %%1 in (e:\findtime.txt) do for /r e: %%a in ("%%3*" do if %%1%%2==2004-02-2315:31 deltree %%a

(Note! When the program window shows you the directory or filename to be deleted and asks for confirmation, you must look carefully at its exact path and make sure it is the one you want to delete. Otherwise, another directory or file with the same name but a different file time may also be deleted!
If you are sure it can be deleted, or there is no other directory or file with the same name but a different file time, then you can add the /y parameter after deltree.
If you absolutely want to delete only directories or files with the specified time, it gets a bit complicated. It would be better to do it in several steps, but in the end you would still have to find them one by one.
Although the above batch file also requires you to confirm them one by one, it only takes a single statement.
If anyone can use a single statement of about the same length to delete only directories or files with the specified time, hurry up and post it!
I really hope Microsoft or someone will design and add a time parameter like the one in xcopy. That would make it much more convenient!)





Floor 2 Posted 2004-02-25 00:00 ·  中国 山西 太原 联通
中级用户
Credits 206
Posts 22
Joined 2004-02-05 00:00
22-year member
UID 16772
Gender Male
Status Offline
Thanks. Just what I needed. Thanks.
Floor 3 Posted 2004-02-25 00:00 ·  中国 重庆 电信
银牌会员
★★★
Credits 2,202
Posts 499
Joined 2003-06-12 00:00
23-year member
UID 4876
Gender Male
Status Offline
Can you talk about the differences and changes between the Win98 window environment and cmd? Thanks
一年四季,枫叶红了又红;人生四季,失去的,还能再来吗?—— !
Floor 4 Posted 2004-02-26 00:00 ·  中国 江苏 扬州 联通
铂金会员
★★★★
C++启程者
Credits 5,154
Posts 1,827
Joined 2003-07-18 00:00
22-year member
UID 7105
Gender Male
Status Offline
In the WIN98 window environment and under pure DOS, the FOR command does not have the /F /R switches from XP/2000, (although you can use %COMSPEC% /C to embed FOR inside FOR), so you can only use other methods.
For example, first DIR E: /S >E:\XXX.TXT and then use FIND or FINDSTR to locate the specified date and time string, thereby finding the directory or filename to be deleted, and then delete them one by one accordingly. It seems very hard to solve it all at once with a single statement, right? (Because DEL /S cannot delete directories, and DELTREE has no /S parameter; it must have the path of the directory or file. That's where the difficulty lies.)





Floor 5 Posted 2004-02-26 00:00 ·  中国 山东 烟台 联通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
Re GOTOmsdos:
For example, first DIR E: /S >E:\XXX.TXT and then use FIND or FINDSTR to locate the specified date and time string, thereby finding the directory or filename to be deleted, and then delete them one by one accordingly. It seems very hard to solve it all at once with a single statement, right? (Because DEL /S cannot delete directories, and DELTREE has no /S parameter; it must have the path of the directory or file. That's where the difficulty lies.)


I'm not very familiar with the NT-series environment, so I'll only talk about the basic idea for implementing deletion of files and directories with a specified date under 98.

In the 98 environment, whether using del or deltree, you need to use the absolute path of the file, unless what you're deleting is a file in the current path. This seems to require either obtaining the absolute path of the file, or jumping to the path where the file is located and deleting it there.

Obtaining the absolute path of the file:
You can first use dir filename /s /b to get the absolute paths of all files, then verify them one by one to see whether they match; the example below uses this method.
http://model.chinajewelry.net/dos/dosbbs/dispbbs.asp?BoardID=6&ID=9588&replyID=59956&skin=1

Jumping to the path where the file is located and deleting it:
This is essentially implementing recursive traversal of files and directories, which is fairly complex. FOR definitely won't do it.




※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Forum Jump: