简体   繁体   English

批处理:确定从文件读取的行中是否存在子字符串

[英]Batch: determine if a substring exist in a line read from a file

I am brand new to batch. 我是一批新手。 My intention is writing a batch that read every line from a file, and depends on the line read in do some different tasks. 我的意图是编写一批从文件中读取每一行的批处理,并依赖于读入的行执行一些不同的任务。 Here is some sample 这是一些样品

@echo off
setlocal enableextensions enabledelayedexpansion

for /f "usebackq delims=" %%a in (test.txt) do ( 
  echo %%a
  *if %%a contains abc do (other tasks)*           
)

In addition, can I detect a "newline" in batch?? 另外,我可以批量检测“换行符”吗? if the test.txt looks like: 如果test.txt看起来像:

123
345
abckdla

abd
abd
abc

test

can I print "this is a new line" when the for loop is at row4 and row8 of test.txt?? 当for循环位于test.txt的row4和row8时,我可以打印“这是新行”吗?

Great thanks to your time. 非常感谢您的时间。

There are two questions in your post 您的帖子中有两个问题

1.- Checking if a variable contains a substring. 1.-检查变量是否包含子字符串。

try this 尝试这个

@echo off
setlocal enableextensions enabledelayedexpansion
for /f "tokens=*" %%a in (test.txt) do ( 
  set tst=%%a
  set tst=!tst:ab=!
  if not !tst!==%%a ( 
    echo %%a contains ab
  ) else (
    echo %%a does not contain ab
  )
)

see HELP SET for more detailed information. 有关更多详细信息,请参见HELP SET

2.- the FOR command skips blank lines. 2.- FOR命令跳过空白行。 Try HELP FOR and read "Blank lines are skipped". 尝试“ HELP FOR然后阅读“跳过空白行”。 There are convoluted solutions involving for example TYPE and FIND I would try to avoid unless strictly necessary. 除非有绝对必要,否则我会尽量避免使用涉及TYPEFIND复杂解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM