Rover12421's Blog

The End.

文本查找替换Bat

一朋友问我有没有好的文本替换工具,问干嘛用,说是要批量替换文本里的某些内容。说这没找到好的,问其具体要求,答
文本1里面查找字段ABC=1
替换到文本2里面字段ABC=2
结果是文本1里面字段ABC=1  不变
文本2里面字段ABC=1   被替换 
汗,难怪说找不到。想来想,用BAT写一给吧

new.ini

1
2
3
4
5
6
7
8
9
10
11
 
[Server Rate]
 
ItemRootRate = 2.0
PlayerExpRate = 1.0
 
[Premium PCRoom Rate]
 
ItemRootRate = 4.0
 
PlayerExpRate = 2.0

server.ini

1
2
3
4
5
6
7
8
9
10
11
12
[Server Rate]
 
ItemRootRate = 2.0
MineSpeedRate = 1.0
 
PlayerExpRate = 1.0
DarkHoleRewardRate = 1.0
[Premium PCRoom Rate]
 
ItemRootRate = 4.0
MineSpeedRate = 2.0
PlayerExpRate = 2.0

用来测试的就是这两给文件来,把server.ini里相应变量的值替换成new.ini对应的值。先下当前目录下建一给文件夹2,把server.ini拷到里面。我下的BAT文件如下:

tf.bat

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
setlocal enabledelayedexpansion
for /f "tokens=1,2,3 delims= " %%a in (new.ini) do (
set sw=%%a
set sw2=%%b
set sw3=%%c
for %%p in (2\*.ini) do (
set files=%%p
for /f "tokens=1,2,3 delims= " %%i in (!files!) do (
set y1=%%i
set y2=%%j
set y3=%%k
if not !sw!==!y1! (echo !y1! !y2! !y3!>>!files!.bak
) else ( echo !sw! !sw2! !sw3! >>!files!.bak
)
echo. >>!files!.bak
  )
del !files!
move !files!.bak !files!
 )
)

测试结果是:
2\server.ini

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[Server Rate]
 
ItemRootRate = 4.0
 
MineSpeedRate = 1.0
 
PlayerExpRate = 2.0
 
DarkHoleRewardRate = 1.0
 
[Premium PCRoom Rate]
 
ItemRootRate = 4.0
 
MineSpeedRate = 2.0
 
PlayerExpRate = 2.0 
 
不过还是有问题的,因为new.ini里的变量有两给值,最后的结果就是最后一个的来。。。。
唉,还真麻烦。要用[]里的字段来分段,先不管来。真累。 
 

Comments