|
xiaoliwind
初级用户
 
积分 43
发帖 32
注册 2005-8-7
状态 离线
|
『楼 主』:
求助:怎样删除D盘下面包含有关键字“sex"的所有文件!
使用 LLM 解释/回答一下
求助:怎样删除D盘下面包含有关键字“sex"的所有文件?
比如我的D盘下面有很多文件。我想删除文件的内容里面包含有”sex" "name"
"year" 只要是其中一个关键字的文件。
Help: How to delete all files under drive D that contain the keyword "sex"? For example, there are many files under my drive D. I want to delete files whose contents contain any one of the keywords "sex", "name", "year".
|
|
2006-9-24 03:07 |
|
|
vkill
金牌会员
     
积分 4103
发帖 1744
注册 2006-1-20 来自 甘肃.临泽
状态 离线
|
『第 2 楼』:
使用 LLM 解释/回答一下
文件的内容里面包含?
这样查找的话速度很慢的
Does the content of the file contain?
Searching like this is very slow
|
|
2006-9-24 03:20 |
|
|
lxmxn
版主
       
积分 11386
发帖 4938
注册 2006-7-23
状态 离线
|
『第 3 楼』:
使用 LLM 解释/回答一下
恩,赞同二楼滴``
这样系统CPU占用极有可能达到100%的``
Well, I agree with the second floor``
In this way, the system CPU usage is very likely to reach 100%``
|
|
2006-9-24 03:33 |
|
|
NaturalJ0
银牌会员
    
积分 1181
发帖 533
注册 2006-8-14
状态 离线
|
『第 4 楼』:
使用 LLM 解释/回答一下
这个用WINDOWS的搜索功能就行了,没必要花心思写个批处理了吧。
This can be done with Windows' search function, there's no need to take the trouble to write a batch script.
|
|
2006-9-24 04:12 |
|
|
namejm
荣誉版主
       batch fan
积分 5226
发帖 1737
注册 2006-3-10 来自 成都
状态 离线
|
『第 5 楼』:
使用 LLM 解释/回答一下
Originally posted by NaturalJ0 at 2006-9-24 04:12:
这个用WINDOWS的搜索功能就行了,没必要花心思写个批处理了吧。
如果只是查找带这些关键字的倒是可以,但是如果要查找文件的内容的话,用windows的搜索功能不会奏效。
Originally posted by NaturalJ0 at 2006-9-24 04:12:
You can just use the search function in Windows, there's no need to take the trouble to write a batch script.
If you just need to find keywords, that's okay, but if you need to search the content of files, the search function in Windows won't work.
|

尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。 |
|
2006-9-24 07:04 |
|
|
3742668
荣誉版主
      
积分 2013
发帖 718
注册 2006-2-18
状态 离线
|
『第 6 楼』:
使用 LLM 解释/回答一下
for /f "skip=1 delims=" %i in ('findstr /m /s /d:d:\ "sex name year" *.txt') do @echo d:\%i
1.以上代码在命令行中运行,如果写到批处理中需把%i替换为%%i
2.如果打印出来的文件列表为正确的结果,可以把echo更改为del来删除文件,建议更改为del后把d:\%i用引号包起来,如: del "d:\%i"
for /f "skip=1 delims=" %i in ('findstr /m /s /d:d:\ "sex name year" *.txt') do @echo d:\%i
1. The above code runs in the command line. If it is written into a batch file, %i needs to be replaced with %%i.
2. If the printed file list is the correct result, you can change echo to del to delete the file. It is recommended to enclose d:\%i in quotes after changing to del, such as: del "d:\%i"
|
|
2006-9-24 07:09 |
|
|
namejm
荣誉版主
       batch fan
积分 5226
发帖 1737
注册 2006-3-10 来自 成都
状态 离线
|
『第 7 楼』:
使用 LLM 解释/回答一下
要实现楼主的愿望,也并非不可能,只是CPU占用会比较高罢了,处理的局限于文本内容的文件罢了,试试下面的代码( 为了安全起见,测试的时候请把 del /a /f "%%i" 换为 echo "%%i"):
@echo off
for /f "tokens=*" %%i in ('dir /a-d /b /s d:\*.txt') do (
findstr /i "sex name year" "%%i">nul && del /a /f "%%i"
)
To achieve the owner's wish, it's not impossible, but the CPU usage will be relatively high. It's limited to processing files with text content. Try the following code ( For safety, when testing, replace del /a /f "%%i" with echo "%%i"):
@echo off
for /f "tokens=*" %%i in ('dir /a-d /b /s d:\*.txt') do (
findstr /i "sex name year" "%%i">nul && del /a /f "%%i"
)
|

尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。 |
|
2006-9-24 07:17 |
|
|
namejm
荣誉版主
       batch fan
积分 5226
发帖 1737
注册 2006-3-10 来自 成都
状态 离线
|
『第 8 楼』:
使用 LLM 解释/回答一下
呵呵,发完帖子之后才发现3742668版主已经在『第 6 楼』回帖了。版主善于充分利用findstr的各个参数,效率非常高,太强了。
Hehe, after posting the message, I just realized that moderator 3742668 has already replied in the '6th floor'. The moderator is good at making full use of various parameters of findstr and is very efficient. It's really powerful.
|

尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。 |
|
2006-9-24 07:26 |
|
|
vkill
金牌会员
     
积分 4103
发帖 1744
注册 2006-1-20 来自 甘肃.临泽
状态 离线
|
『第 9 楼』:
使用 LLM 解释/回答一下
含有关键字“sex"的所有文件
不是只有 txt ? namejm 兄
All files containing the keyword "sex" It's not just txt? Brother namejm
|
|
2006-9-24 07:31 |
|
|
namejm
荣誉版主
       batch fan
积分 5226
发帖 1737
注册 2006-3-10 来自 成都
状态 离线
|
『第 10 楼』:
使用 LLM 解释/回答一下
Originally posted by he200377 at 2006-9-24 07:31:
含有关键字“sex"的所有文件
不是只有 txt ? namejm 兄
没弄明白你的意思。你是想说含有"sex"的所有文件不只是指txt,还是?
Originally posted by he200377 at 2006-9-24 07:31:
All files containing the keyword "sex"
It's not only txt? Brother namejm
I didn't understand what you meant. Do you mean that all files containing "sex" are not only referring to txt, or something else?
|

尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。 |
|
2006-9-24 07:41 |
|
|
pengfei
银牌会员
    
积分 1218
发帖 485
注册 2006-7-21 来自 湖南.娄底
状态 离线
|
『第 11 楼』:
使用 LLM 解释/回答一下
以前一直不敢用find与findstr做工作量大的任务, 原因在于查找命令太耗资源, 速度很慢. 试过在几千个文件中查找指定字符串, 少则几分钟多则十几分钟.
可是看到3742668版主的代码, 很是吃惊. 原来findstr可以配合这些参数来提高效率. 并且效率不是一般的提高. 呵呵~ 学习了...
跟据版主的代码改进以后, 应该可以符合楼主查找多种类型文件的需要.
@echo off
for %%a in (txt doc xml htm) do (
for /f "skip=1 tokens=*" %%i in ('findstr /m /s /d:d:\ "sex name year" *.%%a') do (
echo d:\%%i
)
)
pause
如果还有其他类型请在第一个FOR语句中添加.
I used to be afraid to use find and findstr for tasks with large workloads because the search commands consumed too many resources and were very slow. I tried searching for specified strings in thousands of files, taking at least a few minutes or even more than ten minutes.
But I was very surprised when I saw the code from moderator 3742668. It turns out that findstr can be combined with these parameters to improve efficiency. And the efficiency is improved not just a little. Hehe~ Learned...
After improving according to the moderator's code, it should meet the building owner's need to search for multiple types of files.
@echo off
for %%a in (txt doc xml htm) do (
for /f "skip=1 tokens=*" %%i in ('findstr /m /s /d:d:\ "sex name year" *.%%a') do (
echo d:\%%i
)
)
pause
If there are other types, please add them in the first FOR statement.
|
|
2006-9-24 08:18 |
|
|
3742668
荣誉版主
      
积分 2013
发帖 718
注册 2006-2-18
状态 离线
|
『第 12 楼』:
使用 LLM 解释/回答一下
Re pengfei:
一般来说,在写批处理脚本的时候尽量不要嵌套循环语句,毕竟批处理本身效率就不高。
在本例中,findstr是可以支持多个文件名的,所以可以使用类似下面的代码来提高效率:
@echo off
set "strFiles=*.txt *.bat"
for /f "skip=1 delims=" %%i in ('findstr /m /s /d:d:\ "sex name year" %strFiles%') do echo d:\%%i
pause
当然,也可以把第二句用set /p来接受输入的文件类型。
Re pengfei:
Generally speaking, when writing batch scripts, try not to nest loop statements. After all, batch processing itself is not very efficient.
In this example, findstr can support multiple file names, so you can use code like the following to improve efficiency:
@echo off
set "strFiles=*.txt *.bat"
for /f "skip=1 delims=" %%i in ('findstr /m /s /d:d:\ "sex name year" %strFiles%') do echo d:\%%i
pause
Of course, you can also use set /p in the second sentence to accept the input file type.
|
|
2006-9-24 08:54 |
|
|
pengfei
银牌会员
    
积分 1218
发帖 485
注册 2006-7-21 来自 湖南.娄底
状态 离线
|
『第 13 楼』:
使用 LLM 解释/回答一下
厉害, 没话可说了, 呵呵~
其实如果查找到的匹配文件很多, 批处理效率还是很低的. 如果楼主能提供更多的文件特征, 比如文件名有什么特别. 这样我想会提高不少.
其实Windows搜索功能有按内容查找的功能, 而且速度很快, 可以按文件内容中的某个关键字查找.
3742668版主说的FOR语句嵌套的问题:
我是这样理解循环语句嵌套的. 两个FOR分内循环和外循环, 先执行的是外循环, 把变量放到内循环中, 当内循环执行完毕, 再转到外循环把第二个循环值放到内循环中再次执行内循环. 直到把外循环中的循环值执行完毕, 整个循环也就完成了.
版主的是一次搜索多个扩展名文件, 而嵌套循环是循环搜索多个扩展名. 查找的文件数量是相等的, 我想效率应该也差不多.
Amazing, there's nothing more to say, heh~
In fact, if there are many matching files found, the batch processing efficiency is still very low. If the landlord can provide more file characteristics, such as what's special about the file names. I think this will improve a lot.
In fact, the Windows search function has a function to search by content, and it's very fast, which can search by a certain keyword in the file content.
What the moderator of version 3742668 said about the problem of nested FOR statements:
This is how I understand nested loop statements. The two FORs are divided into inner loop and outer loop. The outer loop is executed first, and the variable is put into the inner loop. When the inner loop is executed, it is transferred back to the outer loop to put the second loop value into the inner loop to execute the inner loop again. Until the loop values in the outer loop are executed, the entire loop is completed.
The moderator's is to search multiple extension files at one time, and the nested loop is to loop and search multiple extensions. The number of files found is equal, and I think the efficiency should be about the same.
|
|
2006-9-24 09:14 |
|
|
vkill
金牌会员
     
积分 4103
发帖 1744
注册 2006-1-20 来自 甘肃.临泽
状态 离线
|
『第 14 楼』:
使用 LLM 解释/回答一下
Originally posted by namejm at 2006-9-24 07:41:
没弄明白你的意思。你是想说含有"sex"的所有文件不只是指txt,还是?
.bat
Originally posted by namejm at 2006-9-24 07:41:
I didn't understand what you meant. Do you mean that all files containing "sex" are not just referring to txt, or something else?
.bat
|
|
2006-9-25 00:59 |
|
|
vkill
金牌会员
     
积分 4103
发帖 1744
注册 2006-1-20 来自 甘肃.临泽
状态 离线
|
『第 15 楼』:
使用 LLM 解释/回答一下
findstr /m /s /d:d:\ "sex name year" %strFiles%
findstr /? 还是没有看懂
findstr /m /s /d:d:\ "sex name year" %strFiles%
findstr /? still don't understand
|
|
2006-9-25 01:03 |
|