Bat - read from file and search if a line contain strings
Moderator: DosItHelp
Bat - read from file and search if a line contain strings
hi guys,
I'm trying to find a way to do the following: I'm reading from a file that has the following structure:
"one two small flowers"
"one two small red apples"
"one two birds singing"
"one two strings bla"
I'm reading line by line and I want to find out if in every line I have found the following strings:
"one two small"
"one two birds"
"one two strings"
but I don't want to take into account "one two small red apples". That is a string I want to exclude from my following logic.
I also want to know the line on which I've found the strings I'm searching for, and to echo found or not found, where if I've found "one two small red apples" - I want to see "not found"
that is what I have up to now:
for /f "delims=" %%b in (%LocalDir%\list.txt) do (
set /a sum=sum+1
echo StringName=%%b
setlocal ENABLEDELAYEDEXPANSION
echo count is= !sum!
endlocal
echo.%StringName%|findstr /c:"one two small" /c:"one two birds" /c:"one two strings" >nul
if %errorlevel%==0 (
echo Found.
)
if %errorlevel%==1 (
echo Not found.
)
)
For some reason I see 'Found' for all records... which is bizarre. I also cannot really image how I can exclude that one record.... :/
Any help would be appreciated. .
Cheers,
Gerry
I'm trying to find a way to do the following: I'm reading from a file that has the following structure:
"one two small flowers"
"one two small red apples"
"one two birds singing"
"one two strings bla"
I'm reading line by line and I want to find out if in every line I have found the following strings:
"one two small"
"one two birds"
"one two strings"
but I don't want to take into account "one two small red apples". That is a string I want to exclude from my following logic.
I also want to know the line on which I've found the strings I'm searching for, and to echo found or not found, where if I've found "one two small red apples" - I want to see "not found"
that is what I have up to now:
for /f "delims=" %%b in (%LocalDir%\list.txt) do (
set /a sum=sum+1
echo StringName=%%b
setlocal ENABLEDELAYEDEXPANSION
echo count is= !sum!
endlocal
echo.%StringName%|findstr /c:"one two small" /c:"one two birds" /c:"one two strings" >nul
if %errorlevel%==0 (
echo Found.
)
if %errorlevel%==1 (
echo Not found.
)
)
For some reason I see 'Found' for all records... which is bizarre. I also cannot really image how I can exclude that one record.... :/
Any help would be appreciated. .
Cheers,
Gerry
Re: Bat - read from file and search if a line contain strin
Code: Select all
@echo off
set "found="
for /f "tokens=1,* delims=[]" %%a in (' find /n /v "" ^< "%LocalDir%\list.txt" ') do (
echo "%%b"|findstr /i /c:"one two small" /c:"one two birds" /c:"one two strings" >nul && set found=1
if defined found echo "%%b"|findstr /v /c:"one two small red apples" >nul || set "found="
if defined found (
echo %%a found
) else (
echo %%a NOT FOUND
)
set "found="
)
pause
Re: Bat - read from file and search if a line contain strin
awesome... it works 
the only thing I cannot really figure out is how to set var1=%%b and var2=%%a, because I want to use them later.
when I try to do this as follow:
if defined found (
echo %%a found
set var1=%%a
set var2=%%b
) else (
echo %%a NOT FOUND
)
nothing actually is stored in var1 and var2.....
when I try to
echo %var1% %var2%
as output I have "Echo is off."

what I want is basically , after I run the first "for" do the following:
@echo off
set "found="
for /f "tokens=1,* delims=[]" %%a in (' find /n /v "" ^< "%LocalDir%\list.txt" ') do (
echo "%%b"|findstr /i /c:"one two small" /c:"one two birds" /c:"one two strings" >nul && set found=1
if defined found echo "%%b"|findstr /v /c:"one two small red apples" >nul || set "found="
if defined found (
echo %%a found
set line_num=%%a
set var=%%b
) else (
echo %%a NOT FOUND
)
set "found="
)
for /f "usebackq %line_num% delims=" %%d in (list1.txt) do (
set "var_char=%%d"
)
echo %var%
echo %var_char%

the only thing I cannot really figure out is how to set var1=%%b and var2=%%a, because I want to use them later.
when I try to do this as follow:
if defined found (
echo %%a found
set var1=%%a
set var2=%%b
) else (
echo %%a NOT FOUND
)
nothing actually is stored in var1 and var2.....
when I try to
echo %var1% %var2%
as output I have "Echo is off."

what I want is basically , after I run the first "for" do the following:
@echo off
set "found="
for /f "tokens=1,* delims=[]" %%a in (' find /n /v "" ^< "%LocalDir%\list.txt" ') do (
echo "%%b"|findstr /i /c:"one two small" /c:"one two birds" /c:"one two strings" >nul && set found=1
if defined found echo "%%b"|findstr /v /c:"one two small red apples" >nul || set "found="
if defined found (
echo %%a found
set line_num=%%a
set var=%%b
) else (
echo %%a NOT FOUND
)
set "found="
)
for /f "usebackq %line_num% delims=" %%d in (list1.txt) do (
set "var_char=%%d"
)
echo %var%
echo %var_char%
Re: Bat - read from file and search if a line contain strin
ok, I think I managed to figure out part of the solution:
I'm confused where the second part is not actually working.. not sure where I'm wrong..
any help will be appreciated.
Code: Select all
@echo off
set "found="
for /f "tokens=1,* delims=[]" %%a in (' find /n /v "" ^< "%LocalDir%\list.txt" ') do (
echo "%%b"|findstr /i /c:"one two small" /c:"one two birds" /c:"one two strings" >nul && set found=1
if defined found echo "%%b"|findstr /v /c:"one two small red apples" >nul || set "found="
if defined found (
echo %%a found
@echo off & setLocal EnableDelayedExpansion
set var=%%b
set Line_num=%%a
endlocal
) else (
echo %%a NOT FOUND
)
set "found="
)
REM part2--------------------
for /f "delims=" %%d in (list1.txt) do (
set FullVersion=%%d
@echo off & setLocal EnableDelayedExpansion
for /f "tokens=1* delims=" %%e in ("%%d") do (
if !Line_num!==%%e
set var2=!FullVersion!
echo !var2!
)
)
endlocal
echo %var%
echo %var2%
I'm confused where the second part is not actually working.. not sure where I'm wrong..
any help will be appreciated.
Re: Bat - read from file and search if a line contain strin
Your "setLocal EnableDelayedExpansion" only applies until the "endlocal" and it's the wrong place to put it if you want to use it in this scenario.
Can you explain what else you want to do?
It's easier than trying to decipher code that doesn't actually work.
Can you explain what else you want to do?
It's easier than trying to decipher code that doesn't actually work.
Re: Bat - read from file and search if a line contain strin
sure,
What I'm actually trying to do is to read from a file (list.txt) and capture a line that matches with at least one of other 3 lines I already know, but doesn't match with specific another one. That's kind of done.
I wan to capture the line I need (matches) in a global var as well as its line number. I need to read the second file (list1.txt) and capture the line that is on the exact same row as the first var.
And I need to use both lines from both texts later - I will assign them to be part of a file name.
That is it.
cheers,
Gerry
What I'm actually trying to do is to read from a file (list.txt) and capture a line that matches with at least one of other 3 lines I already know, but doesn't match with specific another one. That's kind of done.
I wan to capture the line I need (matches) in a global var as well as its line number. I need to read the second file (list1.txt) and capture the line that is on the exact same row as the first var.
And I need to use both lines from both texts later - I will assign them to be part of a file name.
That is it.
cheers,
Gerry
Re: Bat - read from file and search if a line contain strin
It must be very urgent for you to take it to another forum while people are working on your code here.
Re: Bat - read from file and search if a line contain strin
unfortunately, it is indeed 
apologies for the troubles.

apologies for the troubles.
Re: Bat - read from file and search if a line contain strin
gerganag wrote:unfortunately, it is indeed
apologies for the troubles.
The problem with that is there could be more than one solution to your problem. Multiple people on different forums could be trying to a provide you a solution and end up wasting all their time trying to help you and you don't use their solution. That is very discourteous.
Re: Bat - read from file and search if a line contain strin
hi,
Thank you so much for the help. it is greatly appreciated.
I'm posting below my solution (combination of different solutions):
for /f "tokens=1,* delims=[]" %%a in (' find /n /v "" ^< "%LocalDir%\software_list.txt" ') do (
echo "%%b"|findstr /i /c:"Micro Focus Enterprise " /c:"Micro Focus Visual" /c:"Micro Focus COBOL" >nul && set found=1
if defined found echo "%%b"|findstr /v /c:"Micro Focus Enterprise Server for .NET" >nul || set "found="
if defined found (set LineNumber=%%a&set ProductName=%%b)
REM else (echo Main Micro Focus product NOT FOUND. Nothing to do. Exit.&exit /b)
set "found="
)
find "2." temp1.txt > temp3.txt
for /f "tokens=2,3 delims==" %%c in (temp3.txt) do (echo %%c >> %LocalDir%\software_list1.txt)
for /f "tokens=1*delims=[]" %%a in (' find /n /v "" ^< "software_list1.txt" ') do IF %%a==%LineNumber% SET ProductVersion=%%b
set ProductName=%ProductName:"=%
set ProductName=%ProductName: =%
set ProductVersion=%ProductVersion:"=%
set ProductVersion=%ProductVersion: =%
set out_file_name=%ProductName%_%ProductVersion%_%COMPUTER_NAME%
Thank you so much for the help. it is greatly appreciated.
I'm posting below my solution (combination of different solutions):
for /f "tokens=1,* delims=[]" %%a in (' find /n /v "" ^< "%LocalDir%\software_list.txt" ') do (
echo "%%b"|findstr /i /c:"Micro Focus Enterprise " /c:"Micro Focus Visual" /c:"Micro Focus COBOL" >nul && set found=1
if defined found echo "%%b"|findstr /v /c:"Micro Focus Enterprise Server for .NET" >nul || set "found="
if defined found (set LineNumber=%%a&set ProductName=%%b)
REM else (echo Main Micro Focus product NOT FOUND. Nothing to do. Exit.&exit /b)
set "found="
)
find "2." temp1.txt > temp3.txt
for /f "tokens=2,3 delims==" %%c in (temp3.txt) do (echo %%c >> %LocalDir%\software_list1.txt)
for /f "tokens=1*delims=[]" %%a in (' find /n /v "" ^< "software_list1.txt" ') do IF %%a==%LineNumber% SET ProductVersion=%%b
set ProductName=%ProductName:"=%
set ProductName=%ProductName: =%
set ProductVersion=%ProductVersion:"=%
set ProductVersion=%ProductVersion: =%
set out_file_name=%ProductName%_%ProductVersion%_%COMPUTER_NAME%
Re: Bat - read from file and search if a line contain strin
Without having your files or knowledge of what is inside them then this batch code is of use to approximately one person - you.
You must realise that.
You must realise that.