Filter word in a string in batch script

466 Views Asked by At

I created a batch script for windows that I use for mux mkv files. When launch this command:

ffprobe -v 0 -select_streams s -show_entries stream=index:disposition=default -of compact=nk=0 file.mkv | findstr disposition:default=1

Output is:

stream|index=3|disposition:default=1

How can filter and print only number "3" and put it in a variable?

2

There are 2 best solutions below

2
Antares On

Batchfile approach

You need to execute your command inside a for statement inside a batch file to be able to capture the output lines and process them further. Check for /? on the command line and the part with for /f and learn about "usebackq".
The key point is, that you need to escape several special characters from your command, if it is executed in the for statement and not on the command line prompt directly.
Try getting this piece to work and post your solution as update to your answer if you like. Then we can get to the second part of extracting the number.

2
Michele Del Popolo On

I submit a new command that simplify output:

ffprobe -v 0 -select_streams s -show_entries stream=index:disposition=forced:stream_tags=language -of csv=nk=1:p=0 file.mkv | FINDSTR /C:"1,ita"

Output is:

3,1,ita

"3" is track id, "1" is forced flag, "ita" is track language. To create a variable that contains only the track id (e.g. 3) to be inserted in a mkvmerge command, I ran this command:

FOR /F "delims=, tokens=1" %%# IN ('ffprobe -v 0 -select_streams s -show_entries stream=index:disposition=forced:stream_tags=language -of csv=nk=1:p=0 file.mkv ^| FINDSTR /C:"1,ita"') DO SET subid=%%#

But nothing happens! Mkvmerge report this error: Error: Invalid track ID or language code in '-s '.

I don't really know where the mistake is!