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 :eofHere 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: xxxxxxxxxxNAME: "3", DESCR: "WS-C3750-48P"
PID: WS-C3750-48PS-S , VID: V05 , SN: xxxxxxxxxxNAME: "4", DESCR: "WS-C3750G-48PS"
PID: WS-C3750G-48PS-S , VID: V06 , SN: xxxxxxxxxxAnd here is the result I get from Results.txt
192.168.1.1 PID: WS-C3750G-48PS-S , VID: V06 , SN: xxxxxxxxxxAs you can see it only returns the LAST result (NAME:"4") in Results.txt for some reason. Please help me. Thank you
:: ==========================================
::
:: xhaloz.bat Sat 05-03-2011 19:02:29.17
@echo off > newfile & setLocal enableDELAYedeXpansionfor /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
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.
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
Like this? SN
SN
SN
SN
SN
SN
Hostname_1#
Hostname_2#
=====================================
Life is too important to be taken seriously.M2
No not exactly...for example in 1 of the txt files it says
Hostname1:Login
Hostname1:Password
Hostname1: SN BLAH BLAH
Hostname1: SN2 blah blahAnd 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
SN4Since the hostname appears like 4 times in the txt file you would have to make the batch file only store 1 instance of it
Yes (14) | ![]() | |
No (14) | ![]() | |
I don't know (15) | ![]() |