Hi all, I am having some issues trying to write a batch file. Sorry I haven't had much experience with them. Basically what I am trying to do is I have a bunch of files with random names e.g. device1.txt device2.txt device3.txt. I need to search each of those files for 2 different strings within each file and then output those to a file.
e.g. to use findstr to look in device1.txt and find "Hardware" & "Serial Number" and then output those to a text file on the same line. So in the end I should have an output text file with one line for each file that was searched with the 2 results in.
e.g.
device1.txt Hardware: xyz Serial Number 1234
device2.txt Hardware: abc Serial Number 5678
Any help appreciated on this. I have been messing around a bit and not really getting anywhere. Here is an example of what I was trying..
for /F "delims=" %%a in ('findstr /s "Hardware:" *.txt') do (
set var1=%%a
echo %var1% >> %temp%\output.txt
)
Something like that but I need both results shown on one line.
Help!
Thanks
find multiple strings with findstr and output to a text file
Moderator: DosItHelp
Re: find multiple strings with findstr and output to a text
Code: Select all
@echo off
setlocal enableextensions
for /f "delims=" %%f in ('dir /s/b/a-d *.txt') do call :p "%%f"
goto :eof
:p
for /f "delims=" %%a in ('findstr /c:"Hardware" %1') do set "hard=%%a"
for /f "delims=" %%b in ('findstr /c:"Serial Number" %1') do set "sn=%%b"
echo %~nx1 %hard% %sn%>> %temp%\output.txt
goto :eof
Re: find multiple strings with findstr and output to a text file
I'm trying to do something similar to the above .
I want to pull a string , where the line begins with UNB ,from any line in a specific file * There should on be on instance of this in the file***
I also would like to pull another string , where the line begins with BGM ,from any line in a specific file ** note there could be hundreds of these number***
I want to output the results to a new text file
I am using the following
FOR /F "tokens=6 delims=+" %%a IN ('findstr /B /C:UNB D:\atemp\test.txt') DO set "hard=%%a"
FOR /F "tokens=3 delims=+" %%b IN ('findstr /B /C:BGM D:\atemp\test.txt') do set "sn=%%b"
echo %~nx1 %hard% %sn%>> D:\atemp\output.txt
and it is giving me
D:\atemp>FOR /F "tokens=6 delims=+" %a IN ('findstr /B /C:UNB D:\atemp\test.txt'
) DO set "hard=%a"
D:\atemp>set "hard=AVMZMUCD004229"
D:\atemp>FOR /F "tokens=3 delims=+" %b IN ('findstr /B /C:BGM D:\atemp\test.txt'
) do set "sn=%b"
D:\atemp>set "sn=928324960"
D:\atemp>set "sn=928324961"
D:\atemp>set "sn=928324963"
D:\atemp>set "sn=928324964"
D:\atemp>set "sn=928324965"
D:\atemp>set "sn=928324966"
D:\atemp>echo AVMZMUCD004229 928324966 1>>D:\atemp\output.txt
when I run it the result is that I only have one entry
AVMZMUCD004229 928324966
When I was hoping to have
AVMZMUCD004229 928324960
AVMZMUCD004229 928324961
AVMZMUCD004229 928324963
AVMZMUCD004229 928324964
AVMZMUCD004229 928324965
AVMZMUCD004229 928324966
Any advice would be greatly appreciated
I want to pull a string , where the line begins with UNB ,from any line in a specific file * There should on be on instance of this in the file***
I also would like to pull another string , where the line begins with BGM ,from any line in a specific file ** note there could be hundreds of these number***
I want to output the results to a new text file
I am using the following
FOR /F "tokens=6 delims=+" %%a IN ('findstr /B /C:UNB D:\atemp\test.txt') DO set "hard=%%a"
FOR /F "tokens=3 delims=+" %%b IN ('findstr /B /C:BGM D:\atemp\test.txt') do set "sn=%%b"
echo %~nx1 %hard% %sn%>> D:\atemp\output.txt
and it is giving me
D:\atemp>FOR /F "tokens=6 delims=+" %a IN ('findstr /B /C:UNB D:\atemp\test.txt'
) DO set "hard=%a"
D:\atemp>set "hard=AVMZMUCD004229"
D:\atemp>FOR /F "tokens=3 delims=+" %b IN ('findstr /B /C:BGM D:\atemp\test.txt'
) do set "sn=%b"
D:\atemp>set "sn=928324960"
D:\atemp>set "sn=928324961"
D:\atemp>set "sn=928324963"
D:\atemp>set "sn=928324964"
D:\atemp>set "sn=928324965"
D:\atemp>set "sn=928324966"
D:\atemp>echo AVMZMUCD004229 928324966 1>>D:\atemp\output.txt
when I run it the result is that I only have one entry
AVMZMUCD004229 928324966
When I was hoping to have
AVMZMUCD004229 928324960
AVMZMUCD004229 928324961
AVMZMUCD004229 928324963
AVMZMUCD004229 928324964
AVMZMUCD004229 928324965
AVMZMUCD004229 928324966
Any advice would be greatly appreciated
Re: find multiple strings with findstr and output to a text file
Do not set the for variable to an environmental variable in the second FOR command. Just echo the hard variable and the for variable to a file.
Re: find multiple strings with findstr and output to a text file
Will this help?
Code: Select all
For /F "Tokens=6 Delims=+" %%a In ('FindStr/BC:"UNB" D:\atemp\test.txt') Do (
Set "hard=%%a")
( For /F "Tokens=3 Delims=+" %%b In ('FindStr/BC:"BGM" D:\atemp\test.txt'
) Do Echo=%hard% %%b)>>D:\atemp\output.txt
Re: find multiple strings with findstr and output to a text file
Compo wrote:Will this help?Code: Select all
For /F "Tokens=6 Delims=+" %%a In ('FindStr/BC:"UNB" D:\atemp\test.txt') Do (
Set "hard=%%a")
( For /F "Tokens=3 Delims=+" %%b In ('FindStr/BC:"BGM" D:\atemp\test.txt'
) Do Echo=%hard% %%b)>>D:\atemp\output.txt
Thanks Compo! That worked a treat. very much appreciated
Re: find multiple strings with findstr and output to a text file
Code: Select all
@echo off
setlocal EnableDelayedExpansion
set "var="
(for /F "tokens=1* delims=:" %%a in ('findstr /S /C:"Hardware" /C:"Serial Number" *.txt') do (
if not defined var (
set "var=%%b"
) else (
echo %%a !var! %%b
set "var="
)
)) > "%temp%\output.txt"
Antonio