通过循环输入项目的文件来编辑文本文件的特定行

时间:2014-01-07 22:46:19

标签: batch-file

我需要在许多项目上运行一个进程,并且该进程将配置文件作为输入。需要针对具有不同扩展名的三个不同行(1,2,17)处的每个项目更改配置文件。我可以将配置文件作为文本文件读入批处理脚本,但应该使用* .cfg配置保存,以便可以使用它。我已经将以下伪代码与指针放在一起,我需要帮助才能使其运行批处理脚本。

set inputline=1
set outputline=2
set xmlline=17
set infile=config.txt
set items=list.txt


for /f "delims=" %%a in (%items%) do (
echo.%%a

set curr=1
for /f "delims=" %%b in (%infile%) do (

     if !curr!==%inputline%  [replace this line in infile with "a".ext1]
     if !curr!==%outputline%  [replace this line in infile with "a".ext2]
     if !curr!==%xmlline%  [replace this line in infile with "a".ext3]
)
 set /b "curr = curr + 1"
 [save "infile".cfg]


)
 "call" proccess.exe -config.cfg 
)

配置文件:

sample.264            
sample.yuv            
test_rec.yuv             
1                       
1                       
0                       
2                        
500000                   
104000                   
73000                   
leakybucketparam.cfg     
1                        
2                       
2                        
0                       
1                        
sample.xml            
3   

2 个答案:

答案 0 :(得分:0)

Batch没有替换文本文件中单行的内置方法 - 但是您可以读取文件的全部内容,更改行,然后重写文件。

set file=config.txt

setLocal enableDelayedExpansion
set count=0

for /F "delims=~!" %%b in ('type %file%') do (
    set /a count=!count!+1
    set line!count!=%%b
)

set line1="a".ext1
set line2="a".ext2
set line17="a".ext3

del %file%
for /L %%c in (1,1,!count!) do echo !line%%c!>>%file%

pause

我略微修改了我所做的here。我拿出检查来查看文件是否存在,因为你应该可以认为它会存在。

答案 1 :(得分:0)

感谢unclemeat,它帮助我为我制定了以下工作代码:

set file=config.cfg
set vdos=test.txt

setlocal enabledelayedexpansion

for /f "delims=" %%a in (%vdos%) do (
echo %%a

set count=0
for /F "delims=~!" %%b in ('type %file%') do (
set /a count=!count!+1
set line!count!=%%b
)

set line1=%%a%.264
set line2=%%a%.yuv
set line17=%%a%.xml
del %file%
for /L %%c in (1,1,!count!) do echo !line%%c!>>%file%
ldecod config.cfg
)