开发者

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

开发者 https://www.devze.com 2023-04-03 17:56 出处:网络
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

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:

123
345
abckdla

abd
abd
abc

test

can I开发者_JAVA百科 print "this is a new line" when the for loop is at row4 and row8 of test.txt??

Great thanks to your time.


There are two questions in your post

1.- Checking if a variable contains a substring.

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.

2.- the FOR command skips blank lines. Try HELP FOR and read "Blank lines are skipped". There are convoluted solutions involving for example TYPE and FIND I would try to avoid unless strictly necessary.

0

精彩评论

暂无评论...
验证码 换一张
取 消