2

I've been trying all day long to make my work easier by automatizing a flow. Here's what I got so far:

@echo off
setlocal EnableDelayedExpansion

for /l %%x in (1,1,10) do (
        set /a counter=%%x+1
        set output=%%x
        start /wait ffmpeg.exe -i "blabla/playlist.m3u8" -c copy C:\teste\%%x.ts
)
pause >nul

blabla/playlist.m3u8 is always changing (see the below file) and I'd like to rename the stream (link) with an incremented variable so that the first one will become 1.ts, the second 2.ts and so on.

Supposing that my file (streams.txt) is structerd like this:

blabla1/playlist1.m3u8
blabla2/playlist132.m3u8
blabla3/playlist12.m3u8
blabla4/playlist66.m3u8
...

How can I parse each line from the file in my for loop and rename the stream by incrementing the %%x variable ?

// I am aware of the next syntax: for /F "tokens=*" %%A in (myfile.txt) do [process] %%A but I do not know how to actually apply it in my case

3 Answers3

3

Another, simpler way:

@echo off

for /F "tokens=1* delims=:" %%a in ('findstr /N "^" streams.txt') do (
   ECHO start /wait ffmpeg.exe -i "%%b" -c copy C:\teste\%%a.ts
)

The findstr /N command enumerates the lines, so it is not necessary to use another variable for the number. The output is:

start /wait ffmpeg.exe -i "blabla1/playlist1.m3u8" -c copy C:\teste\1.ts
start /wait ffmpeg.exe -i "blabla2/playlist132.m3u8" -c copy C:\teste\2.ts
start /wait ffmpeg.exe -i "blabla3/playlist12.m3u8" -c copy C:\teste\3.ts
start /wait ffmpeg.exe -i "blabla4/playlist66.m3u8" -c copy C:\teste\4.ts

EDIT: New method added as reply to comment

@echo off
setlocal EnableDelayedExpansion

< anotherFile.txt (
   for /F "delims=" %%a in (streams.txt) do (
      set /P "line="
      ECHO start /wait ffmpeg.exe -i "%%a" -c copy "C:\teste\!line!.ts"
   )
)

Output:

start /wait ffmpeg.exe -i "blabla1/playlist1.m3u8" -c copy "C:\teste\first-line-of another-file.ts"
start /wait ffmpeg.exe -i "blabla2/playlist132.m3u8" -c copy "C:\teste\second-line-of another-file.ts"
start /wait ffmpeg.exe -i "blabla3/playlist12.m3u8" -c copy "C:\teste\third-line-of another-file.ts"
start /wait ffmpeg.exe -i "blabla4/playlist66.m3u8" -c copy "C:\teste\fourth-line-of another-file.ts"
Aacini
  • 65,180
  • 12
  • 72
  • 108
  • thanks. Nice solution. What if I'd like instead of numbers to also take a line from another file ? So that the output will be something like `.. copy C:\teste\first-line-of another-file.ts `, `.. copy C:\teste\second-line-of another-file.ts `. –  Jan 06 '16 at 19:17
0

Assuming I understand your objective correctly, this should work:

@ECHO OFF
SETLOCAL EnableDelayedExpansion

SET COUNTER=0
FOR /F "tokens=*" %%G IN ( streams.txt ) DO (
    SET /A COUNTER+=1
    ECHO START /wait ffmpeg.exe -i "%%G" -c copy C:\teste\!COUNTER!.ts
)

The script is inactive, i.e. it just prints out the commands - remove the ECHO to activate it. With your example file streams.txt, it prints:

START /wait ffmpeg.exe -i "blabla1/playlist1.m3u8" -c copy C:\teste\1.ts
START /wait ffmpeg.exe -i "blabla2/playlist132.m3u8" -c copy C:\teste\2.ts
START /wait ffmpeg.exe -i "blabla3/playlist12.m3u8" -c copy C:\teste\3.ts
START /wait ffmpeg.exe -i "blabla4/playlist66.m3u8" -c copy C:\teste\4.ts
zb226
  • 9,586
  • 6
  • 49
  • 79
0

Here is the code needed for reading the text file with the file names line by line and use an incremented number:

@echo off
setlocal EnableDelayedExpansion
set "Counter=0"
for /F "usebackq delims=" %%L in ("streams.txt") do (
    set /A Counter+=1
    ffmpeg.exe -i "%%~L" -c copy C:\teste\!Counter!.ts
)
endlocal
pause >nul

ffmpeg.exe is a console application and therefore no need to use start /wait.

But if you want to use nevertheless command start, you need the line

start "" /wait ffmpeg.exe -i "%%~L" -c copy C:\teste\!Counter!.ts

Read this answer to understand why "" or a meaningful title string specified as first double quoted string is very often required after command start.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • echo /?
  • endlocal /?
  • for /?
  • pause /?
  • set /?
  • setlocal /?

!Counter! instead of %Counter% is required to reference the current value of environment variable Counter with delayed expansion. %Counter% in block defined with ( ... ) would result in replacing this string by 0 as defined above the block on parsing entire block. Run if /? in a command prompt window for details about delayed expansion explained on a simple example.

See also answer on Why is no string output with 'echo %var%' after using 'set var = text' on command line? why assigning a value to a variable should be always done with using double quotes this way: set "variable=value"

Community
  • 1
  • 1
Mofi
  • 46,139
  • 17
  • 80
  • 143