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-08-02 09:08
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Ask for a batch script to delete files View 2,828 Replies 27
Original Poster Posted 2006-11-05 15:10 ·  中国 内蒙古 呼和浩特 电信
初级用户
Credits 50
Posts 23
Joined 2006-11-03 23:05
19-year member
UID 69459
Gender Male
Status Offline
Suppose the directories are as follows:
d:\a\b
d:\a\c\a1
d:\a\c\a2
And there are several files in each of the 3 directories. Now, we want to delete all the files with the earliest creation time in each directory. How to write the command?
Thank you, everyone
Floor 2 Posted 2006-11-05 18:41 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
20-year member
UID 59080
Status Offline

What does the "earlier files" mentioned by the original poster refer to? Do you want to delete the first few files with an earlier creation time? If you want to delete the first few files with an earlier creation time, you can use the following code:

@echo off
setlocal enabledelayedexpansion
set /p num=How many of the earlier created files do you want to delete?
for /f "tokens=1,* delims=:" %%i in ('dir /a-d/tc/b ^| findstr/n . ') do (
set /a a=%%i
if !a! LEQ %num% del %%j
)
endlocal

Floor 3 Posted 2006-11-05 20:09 ·  中国 内蒙古 呼和浩特 电信
初级用户
Credits 50
Posts 23
Joined 2006-11-03 23:05
19-year member
UID 69459
Gender Male
Status Offline
Brother of 2f, I want to delete all files of the earliest creation day, but I don't know the specific number. How should I do it?

Suppose there are 60 files in the directory, with 4 files generated each day, that is, there are files for 15 days. Now I want to delete the 4 files of the earliest day. How should I do it?

If the number of files in the directory is unclear, the number generated each day is unclear and not fixed, and now I want to delete all files of the earliest day. How should I do it?

All the above are based on the file creation time.

[ Last edited by onlykier on 2006-11-5 at 07:35 AM ]
Floor 4 Posted 2006-11-05 21:13 ·  中国 内蒙古 呼和浩特 电信
初级用户
Credits 50
Posts 23
Joined 2006-11-03 23:05
19-year member
UID 69459
Gender Male
Status Offline
Go back to sleep, waiting for the good news from brothers tonight..
Floor 5 Posted 2006-11-05 21:24 ·  中国 内蒙古 呼和浩特 电信
初级用户
Credits 50
Posts 23
Joined 2006-11-03 23:05
19-year member
UID 69459
Gender Male
Status Offline
Got an idea, mark it here, and I'll come back to work on it at night.
Use dir /a-d/tc >>1.txt.
Use a loop to save the first number in each line of 1.txt, which is the time. Set a variable in the loop. Because the first file must be deleted. When the loop reaches the second time, compare the time with the time of the first file. If they are the same, continue the loop and delete; if not, jump out of the loop.
Floor 6 Posted 2006-11-06 07:15 ·  中国 甘肃 甘南藏族自治州 合作市 电信
金牌会员
★★★★
Credits 4,103
Posts 1,744
Joined 2006-01-20 13:00
20-year member
UID 49241
Gender Male
From 甘肃.临泽
Status Offline
Floor 7 Posted 2006-11-06 21:55 ·  中国 内蒙古 呼和浩特 电信
初级用户
Credits 50
Posts 23
Joined 2006-11-03 23:05
19-year member
UID 69459
Gender Male
Status Offline
Brother upstairs, the examples in your link all have parameters, and the question I asked is not like that...
Floor 8 Posted 2006-11-06 22:47 ·  中国 内蒙古 呼和浩特 电信
初级用户
Credits 50
Posts 23
Joined 2006-11-03 23:05
19-year member
UID 69459
Gender Male
Status Offline
Please check why the following code is wrong
@echo off
dir /s /a-d /tc|sort|find "-"|find /v "Sequence"
set c=0
for /f "tokens=1,4 delims= " %%i in ('dir /s /a-d /tc|sort|find "-"|find /v "Sequence"') do (
if c equ 0 (set a=%%i
echo %%j
)else
(
set b=%%i
if "b"=="a" (echo %%j
set a=b)
set /a c+=1))

The syntax of the command is incorrect.

[ Last edited by onlykier on 2006-11-6 at 10:24 AM ]
Floor 9 Posted 2006-11-06 23:35 ·  中国 内蒙古 呼和浩特 电信
初级用户
Credits 50
Posts 23
Joined 2006-11-03 23:05
19-year member
UID 69459
Gender Male
Status Offline
Is anyone here? Waiting online
Floor 10 Posted 2006-11-07 01:35 ·  中国 北京 朝阳区 联通
金牌会员
★★★★
Credits 2,902
Posts 1,147
Joined 2006-09-21 12:00
19-year member
UID 63324
Gender Male
Status Offline
If there is an error when running, remove the "@echo off" and then debug, and you will see the information on which command the system error occurred at : )

In this sentence, is there a special symbol in "dir /s /a-d /tc|sort|find "-"|find /v "Sequence"?
Think about which symbol needs to be escaped with the escape character (^)?
(If under the @echo off state, you will not see more detailed error information)

Please take a look at the help of IF /? again, and then look at your usage of this IF : )
"The ELSE clause must appear on the same line after IF..." Do you see if your usage of IF is not in line with the requirements?

......
Another:
  for ……
if ....) else (.... if ...() )....
Among your so many statements, there are many parentheses, especially the last two " )) " parentheses,
When the amount of code is very large, if the code is not written in a standardized way, whether it is yourself or others, it is simply unbearable to find errors,
And it is not conducive to clarity and debugging...

Another:
You have assigned a value earlier:  set c=0
Then the following...
if c equ 0 (If c is equal to 0, then...) Will this c variable be "recognized" by if?

Do you think there is a difference between if %c% equ 0 and if c equ 0?

[ Last edited by redtek on 2006-11-7 at 01:38 AM ]
    Redtek,一个永远在网上流浪的人……

_.,-*~'`^`'~*-,.__.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._
Floor 11 Posted 2006-11-07 02:47 ·  中国 内蒙古 呼和浩特 电信
初级用户
Credits 50
Posts 23
Joined 2006-11-03 23:05
19-year member
UID 69459
Gender Male
Status Offline
dir /s /a-d /tc|sort|find "-"|find /v "序列"
The meaning of this sentence is
/s displays the files in the current directory and subdirectories
/a-d removes the information of folders from the result
/tc displays by creation time
|sort sorts the result of dir /s/a-d/tc, sorting from earliest to latest creation time
|find "-" filters the result of dir /s /a-d /tc|sort to only show lines with "-"
|find /v "序列" functions as, since there is a line of result that is not desired in the result of dir /s /a-d /tc|sort|find "-", it removes the line with "序列" from the result of dir /s /a-d /tc|sort|find
The result of dir /s /a-d /tc|sort|find "-"|find /v "序列" is similar to the following
2006-10-01 19:49 123,392 asdfasdfasd060921-060927.xls
2006-10-01 19:49 123,904 asdfasdfasd060914-060920.xls
2006-10-01 19:49 161,280 asdfasdfasd060831-060906.xls
2006-10-01 19:49 167,936 asdfasdfasd060907-060913.xls
2006-10-07 19:08 160,768 asdfasdfasd060824-060830.xls
2006-10-07 19:13 124,928 asdfasdfasd060928-061004.xls
2006-10-12 00:17 124,416 asdfasdfasd061005-061011.xls
2006-10-19 08:18 124,928 asdfasdfasd061012-061018.xls
2006-10-26 12:22 124,928 asdfasdfasd061019-061025.xls

The above files are in different directories

The key is the sentence for /f "tokens=1,4 delims= " %%i in ('dir /s /a-d /tc|sort|find "-"|find /v "序列"')
I want to only get the first column and the fourth column in the above result, that is, the time and the file name
But this for shows an error that there should not be | here.
You can test the following sentence
for /f "tokens=1,4 delims= " %%i in ('dir /s /a-d /tc|sort|find "-"|find /v "序列"') do echo %%i

As for the loop body in for, maybe I expressed it poorly, everyone can run the following code
set b=0
for /l %%i in (1,1,4) do (
echo %b%
set /a b+=1)
I want to display
0
1
2
3
4
But it shows as
0
0
0
0
0
Please ask the expert how to do it?

[ Last edited by onlykier on 2006-11-6 at 01:52 PM ]
Floor 12 Posted 2006-11-07 02:51 ·  中国 内蒙古 呼和浩特 电信
初级用户
Credits 50
Posts 23
Joined 2006-11-03 23:05
19-year member
UID 69459
Gender Male
Status Offline
By the way, what's the difference between %%i %i% !i! %i?
Floor 13 Posted 2006-11-07 03:07 ·  中国 北京 朝阳区 联通
金牌会员
★★★★
Credits 2,902
Posts 1,147
Joined 2006-09-21 12:00
19-year member
UID 63324
Gender Male
Status Offline
%%i comes from for %%i in (...) do ... The double % sign is used when running in batch processing, representing a variable.

%i% comes from Set i=Redtek. This form can be used in both batch processing or the command line.

%i comes from directly typing a command in non-batch processing (in the command line), for example: C:\> for %i in (...) do .... It also represents a variable.

%0 %1 .... also represent variables (referring to parameters), the running parameters brought in when running the batch processing. For example: Dir /? This /? represents a parameter.

----------------------------

onlykier develops too fast and has skipped many interesting contents. It is suggested that Brother onlykier must go to see the following post~:)
Then do every line of code from the beginning to the end of the content on the following post~:)

[Simple Index]About SET variable, delayed variable, usage of various symbols...
http://www.cn-dos.net/forum/viewthread.php?tid=24549&fpage=1

In addition: I hope onlykier does every content from the beginning to the end in the link of the above post~:)
Treat it as a forum assignment, hee-hee...

[ Last edited by redtek on 2006-11-7 at 03:11 AM ]
    Redtek,一个永远在网上流浪的人……

_.,-*~'`^`'~*-,.__.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._
Floor 14 Posted 2006-11-07 03:18 ·  中国 内蒙古 呼和浩特 电信
初级用户
Credits 50
Posts 23
Joined 2006-11-03 23:05
19-year member
UID 69459
Gender Male
Status Offline
Brother upstairs! i! How to use this?
And I want to display this result
0
1
2
3
4
How to do it
Floor 15 Posted 2006-11-07 03:28 ·  中国 北京 朝阳区 联通
金牌会员
★★★★
Credits 2,902
Posts 1,147
Joined 2006-09-21 12:00
19-year member
UID 63324
Gender Male
Status Offline
Originally posted by onlykier at 2006-11-7 03:18:
Brother upstairs! How to use!i! here?
And I want to display this result
0
1
2
3
4
How to do it



First method:


@echo %dbg% off

for /L %%i in (0,1,4) do (
echo %%i
)
    Redtek,一个永远在网上流浪的人……

_.,-*~'`^`'~*-,.__.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._
Forum Jump: