Solved Pulling line of text and filename

February 26, 2016 at 06:35:49
Specs: Windows 7
I need to pull a certain line of text from many text files. I am able to do so with this...

:: MYBAT.BAT Usage mybat Folder_Name
@ech off
pushd %*
if exist FileNew.txt del FileNew.txt
for %%j in (*.txt) do type "%%j" | find "uniquestring" >> FileNew.new
ren FileNew.new *.txt
popd
:: End_Of_Batch

Now I need to add to each line of text the filename of where it came from. Any ideas?


See More: Pulling line of text and filename

Report •

#1
February 26, 2016 at 07:09:58
✔ Best Answer
@ech off
pushd %*
if exist FileNew.txt del FileNew.txt
for %%j in (*.txt) do (
  for /F "delims=" %%k in ('type "%%j"^|find "uniquestring"') do (
    echo.%%j  %%k
  ) >> FileNew.new
)
ren FileNew.new *.txt
popd


Report •

#2
February 26, 2016 at 07:33:53
That works, thanks! Is there an easy way to add a comma between the two?

Report •

#3
February 26, 2016 at 07:49:01
Just replace

echo.%%j %%k with echo.%%j,%%k

You can rearrange the echo command as you like where %%j is the filename and %%k the line.

message edited by IVO


Report •
Related Solutions


Ask Question