0

I am trying to write a Windows batch script which can replace text with an incremented counter.

I have a playlist with tv channels and I would like to replace each channel number throughout the playlist.

My playlist has this structure:

#EXTM3U

#EXTINF:-1,ACS NETWORK TV
http://127.0.0.1:6878/ace/getstream?id=***&.mp4
#EXTINF:-1,Ani
http://127.0.0.1:6878/ace/getstream?id=***&.mp4
#EXTINF:-1,Boomerang HD (France)
http://127.0.0.1:6878/ace/getstream?id=***&.mp4
#EXTINF:-1,Boomerаng TV
http://127.0.0.1:6878/ace/getstream?id=***&.mp4

I want to replace #EXTINF:-1, with:

#EXTINF:-1, 1 
#EXTINF:-1, 2 
#EXTINF:-1, 3 
#EXTINF:-1, 4 
etc.

My script have following structure:

findstr /c:"EXTINF" "C:\Users\home\Links\Desktop\TV-Playlist.m3u" | find /c /v "GarbageStringDefNotInYourResults" > C:\Users\home\Links\Desktop\1111.count
set /p VAR=< C:\Users\home\Links\Desktop\1111.count

for /L %%n in (1,1,%VAR%) do (

setlocal enableextensions disabledelayedexpansion
set "search=#EXTINF:-1,"
set "replace=#EXTINF:-1,!%%n! "
set "textFile=C:\Users\home\Links\Desktop\TV-Playlist.m3u"
for /f "delims=" %%i in ('type "%textFile%" ^& break ^> "%textFile%" ') do (
    set "line=%%i"
    setlocal enabledelayedexpansion
    >>"%textFile%" echo(!line:%search%=%replace%!
    endlocal
)
)
g. uzunov
  • 11
  • 2
  • Just to be clear, are you wanting it to read, for example, `#EXTINF:-1, 1 ACS NETWORK TV`, and `#EXTINF:-1, 4 Boomerаng TV` etc.? – Compo Jan 01 '19 at 15:03
  • @Mofi - i paste code, who can try to run. Meanwhile i found solution with gvim and it is working for me. I don't know if this are possible with batch script instead additional software. – g. uzunov Jan 01 '19 at 16:15
  • Using [JREPL.BAT](https://www.dostips.com/forum/viewtopic.php?t=6044): `jrepl "#EXTINF:-1," "$txt=$0 + n++ + ' '" /jq /jbeg "var n=0" /f "input.txt" /o -` – dbenham Jan 02 '19 at 03:22
  • @Compo, thank you for your help about grammatical correction of my question. I appreciate it. – g. uzunov Jan 02 '19 at 07:52

2 Answers2

1

Here is one solution based on your code:

@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "PlayListFile=C:\Users\home\Links\Desktop\TV-Playlist.m3u"
if not exist "%PlayListFile%" goto EndBatch
set "Counter=1"
for /F "delims= eol=" %%I in ('type "%PlayListFile%" ^& break ^> "%PlayListFile%" ') do (
    set "Line=%%I"
    setlocal EnableDelayedExpansion
    if "!Line:#EXTINF:-1,=!" == "!Line!" (
        >>"%PlayListFile%" echo(!Line!
        endlocal
    ) else (
        >>"%PlayListFile%" call echo !Line:#EXTINF:-1,=#EXTINF:-1,%%Counter%% !
        endlocal
        set /A Counter+=1
    )
)
:EndBatch
endlocal

But it removes empty lines from file because FOR always ignores empty lines.

This code keeps also empty lines:

@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "PlayListFile=C:\Users\home\Links\Desktop\TV-Playlist.m3u"
if not exist "%PlayListFile%" goto EndBatch

set "Counter=1"
set "TempFile=%TEMP%\%~n0.tmp"

(for /F "delims= eol=" %%I in ('%SystemRoot%\System32\findstr.exe /N /R "^" "%PlayListFile%"') do (
    set "Line=%%I"
    setlocal EnableDelayedExpansion
    set "Line=!Line:*:=!"
    if not defined Line (
        echo(
        endlocal
    ) else if "!Line:#EXTINF:-1,=!" == "!Line!" (
        echo(!Line!
        endlocal
    ) else (
        call echo !Line:#EXTINF:-1,=#EXTINF:-1,%%Counter%% !
        endlocal
        set /A Counter+=1
    )
))>"%TempFile%"

if not %Counter% == 1 move /Y "%TempFile%" "%PlayListFile%"
if exist "%TempFile%" del "%TempFile%"

:EndBatch
endlocal

For details on this solution see How to read and print contents of text file line by line?

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.

  • del /?
  • echo /?
  • endlocal /?
  • findstr /?
  • for /?
  • goto /?
  • if /?
  • move /?
  • set /?
  • setlocal /?
Mofi
  • 46,139
  • 17
  • 80
  • 143
0

So, if I understand your question, and if I can do one suggest:

Put 1 for to handle the counter and do rename the string, after all, replace the file:

@echo off && setlocal enableextensions enabledelayedexpansion

set _m3u_file_in="%userprofile%\Links\Desktop\TV-Playlist.m3u"
set _m3u_file_out="%userprofile%\Links\Desktop\TV-Playlist_New.m3u"

set _cnt=0&& type nul >!_m3u_file_out!

for /f "tokens=* delims= " %%i in ('type !_m3u_file_in! ^|findstr /lbc:"#EXTINF:-" ^|findstr /r "[0-9][\,]" ') do (

    set "_old_=%%i"&& call set /a _cnt=!_cnt! + 1
    set "_new_=#EXTINF:-1,!_cnt!!_new:~10!"

)
echo/ Total lines replace: !_cnt!
move /y !_m3u_file_out! !_m3u_file_in!
Io-oI
  • 2,514
  • 3
  • 22
  • 29