Findstr在一个文件中,如果找到 [英] Findstr in a file and if find

查看:222
本文介绍了Findstr在一个文件中,如果找到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文件"FileList.txt"从中我正在阅读我想要搜索字符串的文件列表。

假设文件包含5个文件名,那么我需要在所有这5个文件中查找字符串" #void test"。$
如果在任何这些文件中找不到此字符串,则从File"FileList.txt"中删除文件名。 (我们用来查找文件的名称)

I have a file "FileList.txt" from which I am reading a list of files in which I want to search for a string.
Let say file contains 5 file names, then I need to look in all these 5 files for a string "#void test".
If this string is not found in any of these files then delete the files name from File "FileList.txt" (which we used to find the names of the files)

FileList.txt中的文件名是','之后的2个元素所以我使用了令牌2,因为我只想阅读2个令牌。

The file name in FileList.txt is the 2 element after ',' so I used token 2 as I only wanted to read the 2 token.

for / f" tokens = 2 delims =," %% i in(FileList.txt)do(
$
findstr / i" #void test" / F:somelocation\ %% i zh

for /f "tokens=2 delims=," %%i in (FileList.txt) do (
findstr /i "#void test" /F:somelocation\%%i
)

推荐答案

FindStr返回0到ErrorLevel变量,如果它在文件中找到了什么,从这个观察中你应该能够陷阱,如果ErrorLevel是不同于0并做你需要的事情

FindStr return 0 to ErrorLevel variable if it found something, in the file, from this observation you should be able to trap if ErrorLevel is different then 0 and do what ever you need to

for / f" tokens = 2 delims =," %% i in(FileList.txt)do(
$
  findstr / i" #void test" / F:somelocation\ %% i

  if %ERRORLEVEL%GTR 0(

    Echo #void test在somelocation \ %% i中找不到,文件将被删除>> Activity.LOG

    Del somelocation\ %% i

  )否则(
$
    Echo #void test found in somenocation\ %% i>> Activity.LOG

 )

for /f "tokens=2 delims=," %%i in (FileList.txt) do (
  findstr /i "#void test" /F:somelocation\%%i
  If %ERRORLEVEL% GTR 0 (
    Echo #void test Not found in somelocation\%%i, file will be delete >>Activity.LOG
    Del somelocation\%%i
   ) Else (
    Echo #void test found in  somelocation\%%i >>Activity.LOG
  )
)

因为它激进到删除要测试的文件,您可以先移动文件。然后实施你的决赛。

Since it radical to delete a file for testing you can move your file first. Then implement your final.


这篇关于Findstr在一个文件中,如果找到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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