使用IF - Batch文件用另一行替换一行

时间:2014-04-24 06:23:45

标签: windows batch-file if-statement for-loop

我正在尝试使用从FileList.txt读取的行替换load.xml中的一行。 load.xml的内容

<mainheader>
        <InFilePath>D:\Data\All_Inputfiles\oldfile.txt</InFilePath>
</mainheader>

Filelist.txt中

 newfile1.txt
 newfile2.txt

期望每次迭代的输出为

   <mainheader>
            <InFilePath>D:\Data\All_Inputfiles\newfile1.txt</InFilePath>
    </mainheader>

并在下一次迭代中用newfile2.txt替换newfile1.txt。我能够获得最终字符串的原始字符串,但代码的最后一部分是抛出语法错误,即来自 for / f“delims =”%% a in(!INTEXTFILE!)do call:更改“%% a”。

请帮帮我吗? 提前谢谢。

    @echo off
SETLOCAL EnableDelayedExpansion
Set AllInputFile= D:\data\FileList.txt
SET INTEXTFILE=C:\c:\load.xml
set OUTTEXTFILE=D:\data\tmp_out.txt
SET BackupPath=D:\data\backupload.xml
Set TempFile=D:\data\tmp.txt
SET DbgFile=D:\data\debuginfo.txt
 Del !TempFile!
 Del !DbgFile!
 Copy !INTEXTFILE! !BackupPath!
:replace
   findstr /g "InFilePath" !INTEXTFILE!>!TempFile!
   :: set string="        <InFilePath>D:\Data\All_Inputfiles\oldfile.txt</InFilePath>" 
   set /p string=< !TempFile!
   SET PREVFILE_NM=!string:~75,-13!       
  set FinalreplaceLine=!string!  
  set TARG_FILE=%~1
  REM ECHO  "Before Replace FinalreplaceLine "!FinalreplaceLine!>>!DbgFile!
   set FinalreplaceLine=!FinalreplaceLine:%PREVFILE_NM%=%TARG_FILE%! 
   ECHO  "string "!string!>>!DbgFile!
  ECHO  "FinalreplaceLine "!FinalreplaceLine!>>!DbgFile!


  ::string has source string and FinalreplaceLine has target string to replace
  for /f "delims=" %%a in (!INTEXTFILE!) do call :Change "%%a"  
  exit /b

  :Change
  set Text=%~1
   if "%Text%"=="%string%" (
    (echo !FinalreplaceLine!)>> !OUTTEXTFILE!
     ) else (
     (echo !Text!)>> !OUTTEXTFILE!
       )
    exit /b

1 个答案:

答案 0 :(得分:0)

这对你有用吗?:

@echo off
for /f "usebackq delims=" %%a in ("FileList.txt") do (
   call :change "%%a"
   type "load.xml"
   pause
)
echo done
pause
goto :EOF
:change
(
echo ^<mainheader^>
echo        ^<InFilePath^>D:\Data\All_Inputfiles\%~1^</InFilePath^>
echo ^</mainheader^>
) > "load.xml"