Putting if
into for
in general is easy:
for ... do (
if ... (
...
) else if ... (
...
) else (
...
)
)
A for
loop that iterates over lines can be written using /f
switch:
for /f "delims=" %%s in (*.txt) do (
...
)
Regexps are provided by findstr
. It will match against stdin
if no input file is provided. You can redirect output to NUL
so that it doesn't display the found string, and just use its errorlevel
to see if it matched or not (0 means match, non-0 means it didn't). And you can split a string using /f
again. So:
set count=0
for /f "delims=" %%s in (foo.txt) do (
echo %%s | findstr /r xxxx > NUL
if errorlevel 1 (
rem ~~~ Didn't match xxxx ~~~
echo %%s | findstr /r yyyy > NUL
if errorlevel 1 (
rem ~~~ Didn't match yyy ~~~
for /f "delims=; tokens=1,*" %%a in ('echo %%s') do (
set array1[!count!]=%%a
set array2[!count!]=%%b
set /a count+=1
)
)
) else (
echo XXX is found
)
)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…