日期:2014-05-17  浏览次数:20748 次

求教一个bat问题!
有一个文件1.txt
里面的内容是
D:\install
D:\data
D:\MSSQL2008


另外一个XML文件 2.xml

.....

<nodeName>NMSserver</nodeName>
<installDirectory>C:\MSSQL2008</installDirectory>
<privateIP>127.0.0.1</privateIP>
<fileName>C:\data</fileName>
<rootPasswd>admin</rootPasswd>
<installpath>C:\install</installpath>

.....

怎么写一个bat文件把1.txt中的内容读出来对应的替换掉2.xml文件中的内容呢

D:\install —> C:\install
D:\data —> C:\data
D:\MSSQL2008 —> C:\MSSQL2008


请老师们帮忙看下啊,多谢了!没分了,有机会下次一定奉上!!

------解决方案--------------------
BatchFile code
@echo off & setlocal enabledelayedexpansion
set i=1
for /f %%a in (1.txt) do (
  if !i!==1 (set installPath=%%a) else if !i!==2 (set fileName=%%a) else set installDirectory=%%a
  set /a i+=1
)

(for /f %%a in ('findstr /n .* 2.xml') do (
  set line=%%a
  set line=!line:*:=!
  if "!line!"=="" (echo.) else (
  set changed=!line:^<installPath^>=!
  if "!line!" neq "!changed!" (echo ^<installpath^>%installPath%^</installpath^>) else (
    set changed=!line:^<fileName^>=!
    if "!line!" neq "!changed!" (echo ^<fileName^>%fileName%^</fileName^>) else (
      set changed=!line:^<installDirectory^>=!
      if "!line!" neq "!changed!" (echo ^<installDirectory^>%installDirectory%^</installDirectory^>) else (
        echo !line!
      )
    )
  ))
))>3.xml