Solved Adding line numbers of file to output of a string search

Bto laptops / Custom build laptop
March 15, 2017 at 03:24:26
Specs: Windows 10, i3-6100H @ 2.70 GHz
I got Search.bat looking through all the files in the directory for a string.
Now i'd like to add line numbers to the output, but don't know of a way to do so in batch.

Could any of you give an example, or give me a link to a similar forum post?

For the people that are willing to give it a shot:

Search.bat:

if '%*'=='' (exit /b) else set "string=%*"
if not '%string%'=='"%string:~1,-1%"' set string="%string%"
for /f "tokens=*" %%a in ('dir /A-D /B') do for /f "tokens=*" %%A in ('type "%%a" ^| find %string%') do echo %%a: %%A

netscan.bat:

@echo off&set cnt=0&set max=255&for /f "tokens=2-4 delims=:. " %%a in ('ipconfig ^| findstr "gateway"') do set "ip=%%a.%%b.%%c.")&for /f "tokens=2-5 delims=.: " %%A in ('ipconfig ^| findstr "IPv4"') do set YOU=%%A.%%B.%%C.%%D&set wname=%%A&set up=
if '%1'=='-up' set "up=-up"&goto loop
if '%2'=='-up' set "up=-up"&for /f "tokens=1-5 delims=.-" %%A in ("%1") do (set "wname=%%A"&set "ip=%%A.%%B.%%C."&if not '%%D'=='' set "cnt=%%D"&if not '%%E'=='' set "max=%%E")&goto loop
if '%2'=='' if not '%1'=='-up' if not '%1'=='' for /f "tokens=1-5 delims=.-" %%A in ("%1") do (set "wname=%%A"&set "ip=%%A.%%B.%%C."&if not '%%D'=='' set "cnt=%%D"&if not '%%E'=='' set "max=%%E")
:loop
for /f "tokens=2 delims=(%%" %%A in ('ping -4 -n 1 -w 1 %ip%%cnt%') do if '%%A'=='0' (set status=UP&for /f "tokens=3 delims=. " %%a in ('ping -a -4 -n 1 -w 1 %ip%%cnt% ^| findstr "Pingen naar"') do set hostname=%%a) else set "status=DOWN"&set hostname=unknown
if '%hostname%'=='%wname%' set hostname=unknown
if '%ip%%cnt%'=='%YOU%' (echo %status% %YOU% - %hostname% [YOU]) else (if '%up%'=='-up' (if '%status%'=='UP' echo %status% %ip%%cnt% - %hostname%) else (echo %status% %ip%%cnt% - %hostname%))
if '%cnt%'=='%max%' (exit /b) else set /a cnt+=1&goto loop

Input in cmd:

search "=%"

Output:

netscan.bat: @echo off&set cnt=0&set max=255&for /f "tokens=2-4 delims=:. " %%a in ('ipconfig ^| findstr "gateway"') do set "ip=%%a.%%b.%%c.")&for /f "tokens=2-5 delims=.: " %%A in ('ipconfig ^| findstr "IPv4"') do set YOU=%%A.%%B.%%C.%%D&set wname=%%A&set up=
netscan.bat: if '%2'=='-up' set "up=-up"&for /f "tokens=1-5 delims=.-" %%A in ("%1") do (set "wname=%%A"&set "ip=%%A.%%B.%%C."&if not '%%D'=='' set "cnt=%%D"&if not '%%E'=='' set "max=%%E")&goto loop
netscan.bat: if '%2'=='' if not '%1'=='-up' if not '%1'=='' for /f "tokens=1-5 delims=.-" %%A in ("%1") do (set "wname=%%A"&set "ip=%%A.%%B.%%C."&if not '%%D'=='' set "cnt=%%D"&if not '%%E'=='' set "max=%%E")
netscan.bat: for /f "tokens=2 delims=(%%" %%A in ('ping -4 -n 1 -w 1 %ip%%cnt%') do if '%%A'=='0' (set status=UP&for /f "tokens=3 delims=. " %%a in ('ping -a -4 -n 1 -w 1 %ip%%cnt% ^| findstr "Pingen naar"') do set hostname=%%a) else set "status=DOWN"&set hostname=unknown

And I would like the output to be:

netscan.bat: Line 1:: @echo off&set cnt=0&set max=255&for /f "tokens=2-4 delims=:. " %%a in ('ipconfig ^| findstr "gateway"') do set "ip=%%a.%%b.%%c.")&for /f "tokens=2-5 delims=.: " %%A in ('ipconfig ^| findstr "IPv4"') do set YOU=%%A.%%B.%%C.%%D&set wname=%%A&set up=
netscan.bat: Line 3: if '%2'=='-up' set "up=-up"&for /f "tokens=1-5 delims=.-" %%A in ("%1") do (set "wname=%%A"&set "ip=%%A.%%B.%%C."&if not '%%D'=='' set "cnt=%%D"&if not '%%E'=='' set "max=%%E")&goto loop
netscan.bat: Line 4: if '%2'=='' if not '%1'=='-up' if not '%1'=='' for /f "tokens=1-5 delims=.-" %%A in ("%1") do (set "wname=%%A"&set "ip=%%A.%%B.%%C."&if not '%%D'=='' set "cnt=%%D"&if not '%%E'=='' set "max=%%E")
netscan.bat: Line 6: for /f "tokens=2 delims=(%%" %%A in ('ping -4 -n 1 -w 1 %ip%%cnt%') do if '%%A'=='0' (set status=UP&for /f "tokens=3 delims=. " %%a in ('ping -a -4 -n 1 -w 1 %ip%%cnt% ^| findstr "Pingen naar"') do set hostname=%%a) else set "status=DOWN"&set hostname=unknown

Any help would be appreciated

Simple solutions are often the best

message edited by hidde663


See More: Adding line numbers of file to output of a string search

Reply ↓  Report •

#1
March 15, 2017 at 04:50:27
✔ Best Answer
Use FINDSTR /N instead of FIND.

How To Ask Questions The Smart Way


Reply ↓  Report •

#2
March 15, 2017 at 05:02:35
Thanks, didnt know findstr had a switch like that

Simple solutions are often the best


Reply ↓  Report •
Related Solutions


Ask Question