(I'm not going to flag this question, because I'm not a jerk flagger like most other people on Stack Exchange, but just for future reference, questions like this should be posted on Stack Overflow.)
I've actually dealt with the same problem as you before, and unfortunately (from what I know), what you want is not possible in Batch. However, I have a work-around.
Batch is geared more towards running in the CMD, whereas Visual Basic Script is geared more towards running in Powershell. Powershell is capable of doing a lot more complex problems, but because the two are nearly the same thing, they can work together, which is what I'm doing here.
Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("<insert-txt-file-here>", ForReading)
strSearchString = objFile.ReadAll
Set objRegEx = CreateObject("VBScript.RegExp")
objRegEx.Global = True
objRegEx.Pattern = "<insert-pattern-here>"
Set colMatches = objRegEx.Execute(strSearchString)
Const ForWriting = 2
Set obj = CreateObject("Scripting.FileSystemObject")
Set obj1 = obj.OpenTextFile("<insert-output-file-here>", ForWriting)
If colMatches.Count > 0 Then
For Each strMatch in colMatches
The code above will extract any portion of text from a text file, as long as the text matches with it's pattern (insert-pattern-here). For example, if I wanted to search for each time "red jaguar" appeared in the text file, I would insert
objRegEx.Pattern = "red jaguar". The patterns can get even more complex than that, more info here.
I know that the above code has worked for me in the past, but Visual Basic Script is notorious for not working universally, so you might have to mess around with the code a bit. I would test it myself, but my Desktop is broken right now, so I am stuck with a laptop that has linux on it, and linux cannot run Visual Basic Script. Also, in case you don't already know, the script must be saved as a '.vbs' file.
Anyways, if you have some kind of 'master program' or something that runs in Batch, then you can tell the batch file to start the Visual Basic Script using the
START /WAIT command, let the script run, then import it's output using this code:
for /f "Delims=" %%a in (<insert-vbs-output-here>) do (set output=%%a).
Hope this helps.
EDIT: I forgot to mention that the output text file for the Visual Basic Script must be provided in order for it to work correctly. If you want to have a fresh output text file each time you run the script, then before you start the script in the Batch file, create the text file using this:
echo. > output.txt, and then after the script runs and the it's output is imported, use this to delete the text file:
DEL output.txt. However, if you're debugging, it's probably a good idea to not delete the output, just so you can see what it's outputing.