中国DOS联盟论坛

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-13 09:52
47,811 topics / 349,897 posts / today 0 new / 48,256 members
DOS学习入门 & 精彩文章 (教学室) » How to delete directories and files with a specified date and time?? (Looking for improvements from everyone)
Printable Version  2,519 / 4
Floor1 GOTOmsdos Posted 2004-02-23 00:00
铂金会员 Posts 1,827 Credits 5,154
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!)





Floor2 骨灰龙 Posted 2004-02-25 00:00
中级用户 Posts 22 Credits 206
Thanks. Just what I needed. Thanks.
Floor3 xiaojun Posted 2004-02-25 00:00
银牌会员 Posts 499 Credits 2,202
Can you talk about the differences and changes between the Win98 window environment and cmd? Thanks
Floor4 GOTOmsdos Posted 2004-02-26 00:00
铂金会员 Posts 1,827 Credits 5,154
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.)





Floor5 willsort Posted 2004-02-26 00:00
元老会员 Posts 1,512 Credits 4,432
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.




[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023