从文件中读取并将其替换为另一个文件

时间:2014-04-22 18:07:48

标签: windows batch-file

我正在尝试创建一个批处理文件,该文件将从文件中读取并将文本存储在变量中。稍后,此变量必须在另一个XML文件中替换,如下所述

  1. 通过逐行读取,即在另一行
  2. 之后的一行,循环通过Inputfile
  3. 每次都替换从payload.xml上面的步骤中获取的文件名
  4. 继续执行步骤2,直到处理完文件结束。示例输入和输出如下所示
  5. REM Example Input:        D:\Data\somefile.txt REM 1st Iteration Output:       D:\Data\file1.txt REM 2nd Iteration Output:       D:\Data\file2.txt REM and so on

    我正在尝试执行以下操作,但它无法正常工作。请帮助。

    SETLOCAL EnableDelayedExpansion
    Set AllInputFile=D:\AllInputFile\FileList_for_Import.txt
    SET INTEXTFILE=C:\myfolder\payload.xml
    
    ECHO "ERRORLEVEL 0-Sucesss else fail " %ERRORLEVEL% " " %AllInputFile%1>>D:\Data\debuginfo.txt2>>D:\Data\debugerr.txt
    
    ECHO "###########Start of MAIN OF LOOP ############"
    
    for /f "tokens=*" %%a in (%AllInputFile%) do (  
      ECHO "ERRORLEVEL 0-Sucesss else fail " %ERRORLEVEL% " " line=%%a1>>D:\Data\debuginfo.txt2>>D:\Data\debugerr.txt
      set "TARG_FILE=%%a"
      ECHO "ERRORLEVEL 0-Sucesss else fail " %ERRORLEVEL% " " "TARG_FILE "%TARG_FILE%1>>D:\Data\debuginfo.txt2>>D:\Data\debugerr.txt
      REM Get the string and store it in a variable
       for /f "tokens=1*delims=:" %%G in ('findstr /n "^" C:\myfolder\payload.xml') do if %%G equ 2  set "DbgLine=%%H"
       ECHO "ERRORLEVEL 0-Sucesss else fail " %ERRORLEVEL% " " "DbgLine "%DbgLine%1>>D:\Data\debuginfo.txt2>>D:\Data\debugerr.txt 
       SET PREVFILE_NM=%DbgLine:~27,-13%
       ECHO %PREVFILE_NM%
    
         call set NewDbgLine=%%DbgLine:!PREVFILE_NM!=!TARG_FILE!%%
            echo !DbgLine!
          ECHO "ERRORLEVEL 0-Sucesss else fail " %ERRORLEVEL% " " "NewDbgLine "!NewDbgLine!1>>D:\Data\debuginfo.txt2>>D:\Data\debugerr.txt 
        echo "#######################"
         set SEARCHTEXT=%DbgLine%
           echo "***************"
           echo %SEARCHTEXT%
           echo "***************"
           set REPLACETEXT=%NewDbgLine%
            set OUTPUTLINE=
    
             REM Replace the file name
    
                for /f "tokens=1,* delims=¶" %%A in ( '"findstr /n ^^ %INTEXTFILE%"') do (
                SET string=%%A
                 for /f "delims=: tokens=1,*" %%a in ("!string!") do set "string=%%b"
                   if  "!string!" == "" (
                   echo.>>%OUTTEXTFILE%
                   ) else (
                          SET modified=!string:%SEARCHTEXT%=%REPLACETEXT%!
                          echo %modified%
                          echo !modified! >> %OUTTEXTFILE%
                   )
                  )
    
        ECHO "ERRORLEVEL 0-Sucesss else fail " %ERRORLEVEL% " " "End of Main Loop Iteration#######################"1>>D:\Data\debuginfo.txt2>>D:\Data\debugerr.txt  
    )
    

    最诚挚的问候, IND。

1 个答案:

答案 0 :(得分:0)

使用enabledelayedexpansion并在循环中更改变量时 您需要使用!variablename!语法,而不是%variablename%