Batch file find string multiple text docs

March 4, 2011 at 14:41:52
Specs: Windows 7
I've SEARCHED EVERYWHERE I can't find a solution. Basically I have a group of text files that contain serial numbers of switches I own. The batch file I made looks for the keyword "SN" and writes the file name along with the result in a text file called Results.txt. However I have a few text documents that have multiple instances of the word "SN" and the batch file only returns the last result found in the text files. Here is my code

@echo off
setlocal enableextensions
del .\Results.txt
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:"SN" %1') do set "SN=%%a"
echo %~n1 %sn% >> .\Results.txt
goto :eof

Here is a example file it's searching (192.168.1.1.txt)

NAME: "1", DESCR: "WS-C3750-48TS"
PID: WS-C3750-48TS-S , VID: V05 , SN: xxxxxxxxxx
NAME: "GigabitEthernet1/0/1", DESCR: "1000BaseLX SFP"
PID: , VID: , SN: xxxxxxxxxx

NAME: "2", DESCR: "WS-C3750-48P"
PID: WS-C3750-48PS-S , VID: V06 , SN: xxxxxxxxxx

NAME: "3", DESCR: "WS-C3750-48P"
PID: WS-C3750-48PS-S , VID: V05 , SN: xxxxxxxxxx

NAME: "4", DESCR: "WS-C3750G-48PS"
PID: WS-C3750G-48PS-S , VID: V06 , SN: xxxxxxxxxx

And here is the result I get from Results.txt
192.168.1.1 PID: WS-C3750G-48PS-S , VID: V06 , SN: xxxxxxxxxx

As you can see it only returns the LAST result (NAME:"4") in Results.txt for some reason. Please help me. Thank you


See More: Batch file find string multiple text docs

Report •


#1
March 5, 2011 at 04:25:14
:: ==========================================
::
:: xhaloz.bat Sat 05-03-2011 19:02:29.17
@echo off > newfile & setLocal enableDELAYedeXpansion

for /f "tokens=* delims= " %%a in ('dir/b *.txt') do (
echo.=======================
echo.%%a
find "SN" < %%a
) >> newfile


=====================================
Life is too important to be taken seriously.

M2


Report •

#2
March 5, 2011 at 08:23:46
Ok making progress. This works but if I have more than 1 text file, it only returns the results of the last text file. But it IS returning ALL the SN results.

Report •

#3
March 5, 2011 at 08:34:42
Actually just figured it out had my output as > instead of >>. Last question man, say there was a hostname in each text document as well for Example

Hostname_1#
SN
SN
SN
Hostname_2#
SN
SN
SN
And I wanted to put these hostnames from EACH text file to be included with the serial numbers at the top. How could I do that? The delimiter would have to be the #. Thanks in advance


Report •

Related Solutions

#4
March 6, 2011 at 01:12:48
Like this?

SN
SN
SN
SN
SN
SN
Hostname_1#
Hostname_2#


=====================================
Life is too important to be taken seriously.

M2


Report •

#5
March 6, 2011 at 10:15:13
No not exactly...for example in 1 of the txt files it says
Hostname1:Login
Hostname1:Password
Hostname1: SN BLAH BLAH
Hostname1: SN2 blah blah

And in the next file it would say the same just change the number (Hostname2).

I want the batch file to put the hostname along with the SN
So it would be
Hostname1
SN1
SN2
SN3
SN4
===========
Hostname2
SN1
SN2
SN3
SN4

Since the hostname appears like 4 times in the txt file you would have to make the batch file only store 1 instance of it


Report •


Ask Question