Bat - read from file and search if a line contain strings

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
gerganag
Posts: 8
Joined: 04 Jun 2013 02:07

Bat - read from file and search if a line contain strings

#1 Post by gerganag » 05 Jun 2013 01:07

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

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Bat - read from file and search if a line contain strin

#2 Post by foxidrive » 05 Jun 2013 02:58

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

gerganag
Posts: 8
Joined: 04 Jun 2013 02:07

Re: Bat - read from file and search if a line contain strin

#3 Post by gerganag » 05 Jun 2013 05:26

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%

gerganag
Posts: 8
Joined: 04 Jun 2013 02:07

Re: Bat - read from file and search if a line contain strin

#4 Post by gerganag » 05 Jun 2013 09:37

ok, I think I managed to figure out part of the solution:

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.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Bat - read from file and search if a line contain strin

#5 Post by foxidrive » 05 Jun 2013 13:47

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.

gerganag
Posts: 8
Joined: 04 Jun 2013 02:07

Re: Bat - read from file and search if a line contain strin

#6 Post by gerganag » 06 Jun 2013 00:57

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

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Bat - read from file and search if a line contain strin

#7 Post by foxidrive » 06 Jun 2013 04:46

It must be very urgent for you to take it to another forum while people are working on your code here.

gerganag
Posts: 8
Joined: 04 Jun 2013 02:07

Re: Bat - read from file and search if a line contain strin

#8 Post by gerganag » 06 Jun 2013 04:59

unfortunately, it is indeed :(
apologies for the troubles.

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Bat - read from file and search if a line contain strin

#9 Post by Squashman » 06 Jun 2013 05:48

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.

gerganag
Posts: 8
Joined: 04 Jun 2013 02:07

Re: Bat - read from file and search if a line contain strin

#10 Post by gerganag » 11 Jun 2013 04:52

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%

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Bat - read from file and search if a line contain strin

#11 Post by foxidrive » 11 Jun 2013 05:04

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.

Post Reply