搜索然后删除,取决于文件是否包含字符串 [英] Search and then delete depending on whether files contain a string

查看:293
本文介绍了搜索然后删除,取决于文件是否包含字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在单个目录中搜索一个字符串('monkey')的多个文本文件,如果字符串存在的话,取决于哪个最简单:


  1. 重命名匹配的字符串 - 例如改变猴子monkey1然后保存文件并进行搜索/处理

    $ b $或$

    ol start =2>
  2. 删除任何具有匹配字符串的文件。

使用本地Windows批处理命令修改文本文件的内容是相当复杂的,所以我们可以使用本地Windows批处理命令来修改文本内容。选项1)并不容易。如果你下载第三方工具,比如gnu sed for Windows,很容易。

选项2)非常简单。你可以在没有批处理文件的情况下在命令行上执行它。假设您的当前目录是您要查找文件的位置:

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

在批处理文件中,你需要将百分比加倍 - 使用 %% F 而不是%F p>

FINDSTR命令有许多选项,例如/ I用于不区分大小写的搜索,/ S用于搜索子目录,和/ R用于原始正则表达式搜索。 b $ b

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.

解决方案

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.

这篇关于搜索然后删除,取决于文件是否包含字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆