Search and then delete depending on whether files contain a string

旧街凉风 提交于 2020-01-02 05:06:19

问题


I'd like to search through multiple text files in a single directory for a string ('monkey'), if the string exists, then either, depending on what's easiest:

  1. rename the matching string - e.g. change monkey monkey1 and save then file and carry on searching/processing

or

  1. Delete any file that has the matching string.

Have searched but can't seem to find anything straightforward.


回答1:


Modifying the contents of a text file is fairly complex using native Windows batch commands, so option 1) is not easy. Though it is easy if you download a 3rd party tool like gnu sed for Windows.

Option 2) is very easy. You can do it on the command line without a batch file. Assuming your current directory is where you want to look for the files:

for /f "eol=: delims=" %F in ('findstr /m monkey *.txt') do del "%F"

If executed from within a batch file then you need to double up the percents - use %%F instead of %F.

There are many options to the FINDSTR command, such as /I for case insensitive search, /S to search subdirectories, and /R for primitive regex searches.



来源:https://stackoverflow.com/questions/11868825/search-and-then-delete-depending-on-whether-files-contain-a-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!