Reputation: 1746
I am trying read all lines in out.txt
and check if that starts with word No
. If the line starts with No
, I am echoing yes
to file 1.txt
, else no
to 2.txt
I am using below code :
for /F "tokens=*" %%A in (out.txt) do (
IF "%%A:~0,2%"=="No" (
@echo yes >> 1.txt
)else (
@echo no >> 2.txt
)
)
but its nit working for me. seems like the if statement is not working correctly. Can someone please tell me if there is something wrong with the code ?. I tried to echo %%A
and is reading the lines as expected.
Upvotes: 2
Views: 2817
Reputation: 6659
This is faster:
@setlocal ENABLEEXTENSIONS
@set prompt=$G
@for /F "tokens=*" %%A in (out.txt) do @call :DoIt "%%A"
@exit /b 0
:DoIt
@set _line=%~1
@set _linePrefeix=%_line:~0,2%
@if "%_linePrefeix%" equ "No" (@echo yes >> 1.txt) else (@echo no >> 2.txt)
@exit /b 0
Magoo's solution and this one both require two processes, but this one involves an executable image that is already loaded into memory (cmd.exe).
Upvotes: 1
Reputation: 80023
You cannot substring a metavariable.
Try
findstr /b /L "No" out.txt >nul
if errorlevel 1 (
echo not found
) else (
echo found
)
which finds any line in the file that /b
begins /L
with the literal No
and outputs any found. The >nul
disposes of the output. errorlevel
is set to 0
if the string was found, non-zero otherwise.
Upvotes: 3