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.
精彩评论