Loops 逐行分析文件,并在for循环中批量递增一个变量

Loops 逐行分析文件,并在for循环中批量递增一个变量,loops,batch-file,Loops,Batch File,我一整天都在试图通过自动化流程来简化我的工作。以下是我目前得到的信息: @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 &g

我一整天都在试图通过自动化流程来简化我的工作。以下是我目前得到的信息:

@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
总是在变化(见下面的文件),我想用一个递增的变量重命名流(链接),这样第一个变量将变成
1.ts
,第二个
2.ts
,依此类推

假设我的文件(
streams.txt
)的结构如下:

blabla1/playlist1.m3u8
blabla2/playlist132.m3u8
blabla3/playlist12.m3u8
blabla4/playlist66.m3u8
...
如何解析
for
循环中文件中的每一行,并通过增加
%%x
变量来重命名流


//我知道下一个语法:
for/F“tokens=*”%%A in(myfile.txt)do[process]%A
,但我不知道如何在我的案例中实际应用它

假设我正确理解您的目标,这应该可以:

@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
)
脚本处于非活动状态,即仅打印命令-删除
ECHO
以激活它。使用示例文件
streams.txt
,它将打印:

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

以下是逐行读取文件名为的文本文件并使用递增数字所需的代码:

@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
是一个控制台应用程序,因此无需使用
start/wait

但是,如果要使用命令start,则需要该行

start "" /wait ffmpeg.exe -i "%%~L" -c copy C:\teste\!Counter!.ts
阅读以了解为什么在命令start之后经常需要指定为第一个双引号字符串的
或有意义的标题字符串

要了解所使用的命令及其工作方式,请打开命令提示符窗口,在其中执行以下命令,并非常仔细地阅读为每个命令显示的所有帮助页面

  • echo/?
  • endlocal/?
  • 获取/?
  • 暂停/?
  • 设置/?
  • setlocal/?
!柜台而不是
%Counter%
来引用延迟扩展的环境变量
Counter
的当前值<用
定义的块中的code>%Counter%
..
将导致在解析整个块时将该字符串替换为块上方定义的
0
。在命令提示窗口中运行
if/?
,以获取有关在一个简单示例中解释的延迟扩展的详细信息

另请参见关于为什么应始终使用双引号为变量赋值的答案:
设置“variable=value”
另一种更简单的方法:

@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
)
findstr/N
命令枚举行,因此不必为数字使用另一个变量。输出为:

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
编辑:添加新方法作为回复评论


谢谢很好的解决方案。如果我想从另一个文件中取一行,而不是数字,该怎么办?因此,输出类似于
。。复制C:\teste\other-file.ts的第一行
。。复制C:\teste\other-file.ts的第二行
@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"
   )
)
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"