查找文本并将其替换为增量计数器



我正在尝试编写一个Windows批处理脚本,该脚本可以用递增的计数器替换文本。

我有一个带有电视频道的播放列表,我想替换整个播放列表中的每个频道号。

我的播放列表具有以下结构:

#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

我想将#EXTINF:-1,替换为:

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

我的脚本具有以下结构:

findstr /c:"EXTINF" "C:UsershomeLinksDesktopTV-Playlist.m3u" | find /c /v "GarbageStringDefNotInYourResults" > C:UsershomeLinksDesktop1111.count
set /p VAR=< C:UsershomeLinksDesktop1111.count
for /L %%n in (1,1,%VAR%) do (
setlocal enableextensions disabledelayedexpansion
set "search=#EXTINF:-1,"
set "replace=#EXTINF:-1,!%%n! "
set "textFile=C:UsershomeLinksDesktopTV-Playlist.m3u"
for /f "delims=" %%i in ('type "%textFile%" ^& break ^> "%textFile%" ') do (
    set "line=%%i"
    setlocal enabledelayedexpansion
    >>"%textFile%" echo(!line:%search%=%replace%!
    endlocal
)
)

下面是一个基于您的代码的解决方案:

@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "PlayListFile=C:UsershomeLinksDesktopTV-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

但它从文件中删除空行,因为 FOR 始终忽略空行。

此代码还保留空行:

@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "PlayListFile=C:UsershomeLinksDesktopTV-Playlist.m3u"
if not exist "%PlayListFile%" goto EndBatch
set "Counter=1"
set "TempFile=%TEMP%%~n0.tmp"
(for /F "delims= eol=" %%I in ('%SystemRoot%System32findstr.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

有关此解决方案的详细信息,请参阅如何逐行读取和打印文本文件的内容?

要了解使用的命令及其工作原理,请打开命令提示符窗口,在那里执行以下命令,并仔细阅读为每个命令显示的所有帮助页面。

  • del /?
  • echo /?
  • endlocal /?
  • findstr /?
  • for /?
  • goto /?
  • if /?
  • move /?
  • set /?
  • setlocal /?

所以,如果我理解你的问题,如果我能做一个建议:

输入 1 for 来处理计数器并重命名字符串,毕竟替换文件:

@echo off && setlocal enableextensions enabledelayedexpansion
set _m3u_file_in="%userprofile%LinksDesktopTV-Playlist.m3u"
set _m3u_file_out="%userprofile%LinksDesktopTV-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!

最新更新