![]() |
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-09-15 12:31 |
47,812 topics / 349,917 posts / today 0 new / 48,268 members |
| DOS批处理 & 脚本技术(批处理室) » Help: How to delete all files containing the keyword "sex" under drive D! |
| Printable Version 4,484 / 18 |
| Floor1 xiaoliwind | Posted 2006-09-24 03:07 |
| 初级用户 Posts 32 Credits 43 | |
|
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".
|
|
| Floor2 vkill | Posted 2006-09-24 03:20 |
| 金牌会员 Posts 1,744 Credits 4,103 From 甘肃.临泽 | |
|
Does the content of the file contain?
Searching like this is very slow |
|
| Floor3 lxmxn | Posted 2006-09-24 03:33 |
| 版主 Posts 4,938 Credits 11,386 | |
|
Well, I agree with the second floor``
In this way, the system CPU usage is very likely to reach 100%`` |
|
| Floor4 NaturalJ0 | Posted 2006-09-24 04:12 |
| 银牌会员 Posts 533 Credits 1,181 | |
|
This can be done with Windows' search function, there's no need to take the trouble to write a batch script.
|
|
| Floor5 namejm | Posted 2006-09-24 07:04 |
| 荣誉版主 Posts 1,737 Credits 5,226 From 成都 | |
Originally posted by NaturalJ0 at 2006-9-24 04:12: 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. |
|
| Floor6 3742668 | Posted 2006-09-24 07:09 |
| 荣誉版主 Posts 718 Credits 2,013 | |
|
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" |
|
| Floor7 namejm | Posted 2006-09-24 07:17 |
| 荣誉版主 Posts 1,737 Credits 5,226 From 成都 | |
|
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"):
|
|
| Floor8 namejm | Posted 2006-09-24 07:26 |
| 荣誉版主 Posts 1,737 Credits 5,226 From 成都 | |
|
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.
|
|
| Floor9 vkill | Posted 2006-09-24 07:31 |
| 金牌会员 Posts 1,744 Credits 4,103 From 甘肃.临泽 | |
|
All files containing the keyword "sex" It's not just txt? Brother namejm
|
|
| Floor10 namejm | Posted 2006-09-24 07:41 |
| 荣誉版主 Posts 1,737 Credits 5,226 From 成都 | |
Originally posted by he200377 at 2006-9-24 07:31: I didn't understand what you meant. Do you mean that all files containing "sex" are not only referring to txt, or something else? |
|
| Floor11 pengfei | Posted 2006-09-24 08:18 |
| 银牌会员 Posts 485 Credits 1,218 From 湖南.娄底 | |
|
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. |
|
| Floor12 3742668 | Posted 2006-09-24 08:54 |
| 荣誉版主 Posts 718 Credits 2,013 | |
|
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: Of course, you can also use set /p in the second sentence to accept the input file type. |
|
| Floor13 pengfei | Posted 2006-09-24 09:14 |
| 银牌会员 Posts 485 Credits 1,218 From 湖南.娄底 | |
|
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. |
|
| Floor14 vkill | Posted 2006-09-25 00:59 |
| 金牌会员 Posts 1,744 Credits 4,103 From 甘肃.临泽 | |
Originally posted by namejm at 2006-9-24 07:41: .bat |
|
| Floor15 vkill | Posted 2006-09-25 01:03 |
| 金牌会员 Posts 1,744 Credits 4,103 From 甘肃.临泽 | |
|
findstr /m /s /d:d:\ "sex name year" %strFiles%
findstr /? still don't understand |
|
| 1 2 Next |
|
[ Contact the Union admin team -
中国DOS联盟 -
Standard version ] Sponsored by ifanr Inc | © 2001–2023 |