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-07-21 19:00
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Help: How to delete all files containing the keyword "sex" under drive D! View 4,138 Replies 18
Original Poster Posted 2006-09-24 03:07 ·  中国 浙江 温州 电信
初级用户
Credits 43
Posts 32
Joined 2005-08-07 12:05
20-year member
UID 41425
Status Offline
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".
Floor 2 Posted 2006-09-24 03:20 ·  中国 甘肃 张掖 电信
金牌会员
★★★★
Credits 4,103
Posts 1,744
Joined 2006-01-20 13:00
20-year member
UID 49241
Gender Male
From 甘肃.临泽
Status Offline
Does the content of the file contain?

Searching like this is very slow
Floor 3 Posted 2006-09-24 03:33 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline
Well, I agree with the second floor``

In this way, the system CPU usage is very likely to reach 100%``
Floor 4 Posted 2006-09-24 04:12 ·  中国 江苏 苏州 电信
银牌会员
★★★
Credits 1,181
Posts 533
Joined 2006-08-14 12:54
19-year member
UID 60484
Status Offline
This can be done with Windows' search function, there's no need to take the trouble to write a batch script.
Floor 5 Posted 2006-09-24 07:04 ·  中国 广东 佛山 广东睿江科技有限公司
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
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没商量。
考虑问题复杂化,解决问题简洁化。
Floor 6 Posted 2006-09-24 07:09 ·  中国 湖北 荆门 电信
荣誉版主
★★★
Credits 2,013
Posts 718
Joined 2006-02-18 07:07
20-year member
UID 50550
Status Offline
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"
Floor 7 Posted 2006-09-24 07:17 ·  中国 广东 佛山 广东睿江科技有限公司
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
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没商量。
考虑问题复杂化,解决问题简洁化。
Floor 8 Posted 2006-09-24 07:26 ·  中国 广东 佛山 广东睿江科技有限公司
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
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没商量。
考虑问题复杂化,解决问题简洁化。
Floor 9 Posted 2006-09-24 07:31 ·  中国 甘肃 张掖 电信
金牌会员
★★★★
Credits 4,103
Posts 1,744
Joined 2006-01-20 13:00
20-year member
UID 49241
Gender Male
From 甘肃.临泽
Status Offline
All files containing the keyword "sex" It's not just txt? Brother namejm
Floor 10 Posted 2006-09-24 07:41 ·  中国 广东 佛山 广东睿江科技有限公司
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
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没商量。
考虑问题复杂化,解决问题简洁化。
Floor 11 Posted 2006-09-24 08:18 ·  中国 湖南 娄底 新化县 电信
银牌会员
★★★
Credits 1,218
Posts 485
Joined 2006-07-21 21:24
19-year member
UID 58987
From 湖南.娄底
Status Offline
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.
Floor 12 Posted 2006-09-24 08:54 ·  中国 湖北 荆门 电信
荣誉版主
★★★
Credits 2,013
Posts 718
Joined 2006-02-18 07:07
20-year member
UID 50550
Status Offline
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.
Floor 13 Posted 2006-09-24 09:14 ·  中国 湖南 娄底 新化县 电信
银牌会员
★★★
Credits 1,218
Posts 485
Joined 2006-07-21 21:24
19-year member
UID 58987
From 湖南.娄底
Status Offline
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.
Floor 14 Posted 2006-09-25 00:59 ·  中国 甘肃 张掖 电信
金牌会员
★★★★
Credits 4,103
Posts 1,744
Joined 2006-01-20 13:00
20-year member
UID 49241
Gender Male
From 甘肃.临泽
Status Offline
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
Floor 15 Posted 2006-09-25 01:03 ·  中国 甘肃 张掖 电信
金牌会员
★★★★
Credits 4,103
Posts 1,744
Joined 2006-01-20 13:00
20-year member
UID 49241
Gender Male
From 甘肃.临泽
Status Offline
findstr /m /s /d:d:\ "sex name year" %strFiles%

findstr /? still don't understand
Forum Jump: