|
onlykier
初级用户
 
积分 50
发帖 23
注册 2006-11-3
状态 离线
|
『楼 主』:
问个删除文件的批处理
使用 LLM 解释/回答一下
假设目录如下
d:\a\b
d:\a\c\a1
d:\a\c\a2
而且3个目录下都有若干文件,现在想删除每个目录下产生时间最早的所有文件,请问如何写命令?
谢谢各位
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
|
|
2006-11-5 15:10 |
|
|
lxmxn
版主
       
积分 11386
发帖 4938
注册 2006-7-23
状态 离线
|
『第 2 楼』:
使用 LLM 解释/回答一下
楼主所说的"较早的文件"是指什么时候的文件呢?还是想删除创建时间较早的前几个文件呢?如果要删除创建时间较早的前几个可以用以下的代码:
@echo off
setlocal enabledelayedexpansion
set /p num=要删除创建较早的前几个文件?
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
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
|
|
2006-11-5 18:41 |
|
|
onlykier
初级用户
 
积分 50
发帖 23
注册 2006-11-3
状态 离线
|
『第 3 楼』:
使用 LLM 解释/回答一下
2f的兄弟,我是想删除创建时间最早的那一天的所有文件,但是却不知道具体的数目,请问该怎么做?
假设目录下有60个文件,每天有4个文件产生,也就是说存有15天的文件,我现在想删掉最早那天的4个文件,请问该如何做?
如果目录下文件数数目不清,每天产生的数目不清楚而且不固定,我现在想删掉最早那天的所有文件,请问该如何做?
上面全是以文件创建时间为依据
Last edited by onlykier on 2006-11-5 at 07:35 AM ]
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 ]
|
|
2006-11-5 20:09 |
|
|
onlykier
初级用户
 
积分 50
发帖 23
注册 2006-11-3
状态 离线
|
『第 4 楼』:
使用 LLM 解释/回答一下
回去睡了,晚上等兄弟们的好消息..
Go back to sleep, waiting for the good news from brothers tonight..
|
|
2006-11-5 21:13 |
|
|
onlykier
初级用户
 
积分 50
发帖 23
注册 2006-11-3
状态 离线
|
『第 5 楼』:
使用 LLM 解释/回答一下
有个思路,放这标下,晚上在来搞
用dir /a-d/tc >>1.txt
用个循环将1.txt的每行的第一个数即时间保存,在循环里设置一个变量,因为第一个文件肯定是要删除的 ,当循环进行到第二次时,用时间与第一个文件的时间比较,相同则继续循环并作删除,不相同则跳出循环。
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.
|
|
2006-11-5 21:24 |
|
|
vkill
金牌会员
     
积分 4103
发帖 1744
注册 2006-1-20 来自 甘肃.临泽
状态 离线
|
|
2006-11-6 07:15 |
|
|
onlykier
初级用户
 
积分 50
发帖 23
注册 2006-11-3
状态 离线
|
『第 7 楼』:
使用 LLM 解释/回答一下
楼上的兄弟,你的链接里的例子都要有参数,我问的问题不是那样的。。。
Brother upstairs, the examples in your link all have parameters, and the question I asked is not like that...
|
|
2006-11-6 21:55 |
|
|
onlykier
初级用户
 
积分 50
发帖 23
注册 2006-11-3
状态 离线
|
『第 8 楼』:
使用 LLM 解释/回答一下
请各位看看下面的代码为什么会出错
@echo off
dir /s /a-d /tc|sort|find "-"|find /v "序列"
set c=0
for /f "tokens=1,4 delims= " %%i in ('dir /s /a-d /tc|sort|find "-"|find /v "序列"') 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))
命令语法不正确。
Last edited by onlykier on 2006-11-6 at 10:24 AM ]
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 ]
|
|
2006-11-6 22:47 |
|
|
onlykier
初级用户
 
积分 50
发帖 23
注册 2006-11-3
状态 离线
|
|
2006-11-6 23:35 |
|
|
redtek
金牌会员
     
积分 2902
发帖 1147
注册 2006-9-21
状态 离线
|
『第 10 楼』:
使用 LLM 解释/回答一下
如果运行出错了,就把 @echo off 这个去掉再调试,就会看到系统提示在哪一个命令上出错的信息:)
这句里 dir /s /a-d /tc|sort|find "-"|find /v "序列 里面是不是有特殊符号?
想想看哪一个符号是需要脱逸符(脱字符)(^)来转义的?
(如果 @echo off 状态下,将看不到更详细的错误信息)
请再看一下 IF /? 的帮助,再看一下你的这个IF的用法:)
“ELSE 子句必须在 IF 之后出现在同一行上……”,你看一下你的IF的用法是不是不太符合要求?
……
另:
for ……
if ....) else (.... if ...() )....
在你这么多语句当中,有很多的括号,尤其是最后这两个“ )) ”括号,
当代码量非常多的情况下,如果书写代码不规范,无论是自己还是别人找起错误来简直是不能忍受的事情,
而且更不利于清晰易懂和除错……
另:
你前面都赋值了: set c=0
那么后面的……
if c equ 0 (如果 c 等于0,则……) 这个 c 变量 if 会“认识”吗?
if %c% equ 0 和 if c equ 0 你认为有区别吗?
Last edited by redtek on 2006-11-7 at 01:38 AM ]
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,一个永远在网上流浪的人……
_.,-*~'`^`'~*-,.__.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._ |
|
2006-11-7 01:35 |
|
|
onlykier
初级用户
 
积分 50
发帖 23
注册 2006-11-3
状态 离线
|
『第 11 楼』:
使用 LLM 解释/回答一下
dir /s /a-d /tc|sort|find "-"|find /v "序列"
这句话的意思是
/s 显示当前目录及子目录的文件
/a-d 结果中去除文件夹的信息
/tc 按创建时间显示
|sort 是对dir /s/a-d/tc 的结果进行排序,按创建时间从早到晚排序
|find "-" 对dir /s /a-d /tc|sort 的结果进行筛选,只显示带有"-"的行
|find /v "序列" 的作用是,由于dir /s /a-d /tc|sort|find "-"显示的结果中有一行结果是不相要的,所以对dir /s /a-d /tc|sort|find 的结果中去除带有"序列"的那行
dir /s /a-d /tc|sort|find "-"|find /v "序列" 结果类此如下
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
其中上面的文件是在不同目录中的
关键是for /f "tokens=1,4 delims= " %%i in ('dir /s /a-d /tc|sort|find "-"|find /v "序列"') 这句
我想只取上面的结果中的第一列和第四列,即时间和文件名
但是这个for 却显示错误 此时不应有 |。
大家可以试验下面的句子就可以了
for /f "tokens=1,4 delims= " %%i in ('dir /s /a-d /tc|sort|find "-"|find /v "序列"') do echo %%i
至于for中的循环体,可能是我表达不好,大家可以运行下面的代码
set b=0
for /l %%i in (1,1,4) do (
echo %b%
set /a b+=1)
我想显示
0
1
2
3
4
但是却显示成
0
0
0
0
0
请问大虾 该如何做?
Last edited by onlykier on 2006-11-6 at 01:52 PM ]
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 ]
|
|
2006-11-7 02:47 |
|
|
onlykier
初级用户
 
积分 50
发帖 23
注册 2006-11-3
状态 离线
|
『第 12 楼』:
使用 LLM 解释/回答一下
顺便问下 %%i %i% !i! %i 有什么区别?
By the way, what's the difference between %%i %i% !i! %i?
|
|
2006-11-7 02:51 |
|
|
redtek
金牌会员
     
积分 2902
发帖 1147
注册 2006-9-21
状态 离线
|
『第 13 楼』:
使用 LLM 解释/回答一下
%%i 出自于 for %%i in (...) do ... 双%号在批处理中运行使用,代表变量
%i% 出自于 Set i=Redtek 这种形式在批处理或命令行都可以使用
%i 出自于直接在非批处理(在命令行中)直接键入的命令,
如: C:\> for %i in (...) do .... 它同样代表变量
%0 %1 .... 也代表变量(指参数),运行批处理时所带入的运行参数
如:Dir /? 这个 /? 就代表参数
----------------------------------
onlykier开发速度太快,已经跳过了很多好玩的内容,
建议onlykier兄一定要去看下面这个贴子~:)
然后把下面贴子上的内容从头到尾做一遍,任何一行代码全都做一遍:)
[简单索引]关于SET变量、延时变量、各种符号的用法……
http://www.cn-dos.net/forum/viewthread.php?tid=24549&fpage=1
另:希望onlykier在上面贴子的链接中所指的内容,从头到尾全做一遍~:)
当做论坛作业了啊,嘻嘻……
Last edited by redtek on 2006-11-7 at 03:11 AM ]
%%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,一个永远在网上流浪的人……
_.,-*~'`^`'~*-,.__.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._ |
|
2006-11-7 03:07 |
|
|
onlykier
初级用户
 
积分 50
发帖 23
注册 2006-11-3
状态 离线
|
『第 14 楼』:
使用 LLM 解释/回答一下
楼上的兄弟 !i! 这个怎么用阿?
还有我想显示这个结果
0
1
2
3
4
该怎么做
Brother upstairs! i! How to use this?
And I want to display this result
0
1
2
3
4
How to do it
|
|
2006-11-7 03:18 |
|
|
redtek
金牌会员
     
积分 2902
发帖 1147
注册 2006-9-21
状态 离线
|
『第 15 楼』:
使用 LLM 解释/回答一下
Originally posted by onlykier at 2006-11-7 03:18:
楼上的兄弟 !i! 这个怎么用阿?
还有我想显示这个结果
0
1
2
3
4
该怎么做
第一种方法:
@echo %dbg% off
for /L %%i in (0,1,4) do (
echo %%i
)
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,一个永远在网上流浪的人……
_.,-*~'`^`'~*-,.__.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._ |
|
2006-11-7 03:28 |
|