我正在尝试编写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:\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
)
)
答案 0 :(得分:1)
这是一种基于您的代码的解决方案:
@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
但是它会从文件中删除空行,因为 FOR 始终会忽略空行。
此代码还保留空行:
@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
有关此解决方案的详细信息,请参见How to read and print contents of text file line by line?
要了解所使用的命令及其工作方式,请打开命令提示符窗口,在其中执行以下命令,并非常仔细地阅读每个命令显示的所有帮助页面。
del /?
echo /?
endlocal /?
findstr /?
for /?
goto /?
if /?
move /?
set /?
setlocal /?
答案 1 :(得分:0)
因此,如果我理解您的问题,并且我可以提出一个建议:
放置1用于处理计数器并重命名字符串,毕竟替换文件:
@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!