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

请问用replace怎么替换文本内容?
如题,需要用batch脚本写一个文本替换。
已经有了一个参考的程序,但是我不理解它是怎么替换的。也查不到replace这样的用法:
replace %%i tmp %oldvalue% %newvalue%

这里 %%i是循环中的文件名, tmp为一个临时文件。 我理解是把%%i这个文件中的 oldvalue替换为newvalue并保存在tmp中。 可是我试着写了一个得到如下错误:

D:\temp>type ttt1.txt
test1
test2
test3

D:\temp>replace ttt1.txt tmp test1 xxx1
Invalid switch - test1
No files replaced

请问为啥总是 Invalid switch? 

另外还有一个问题,放一起问了。。 
if "!errorlevel!" == "1"     
这里的两个 ! 是什么意思? 谢谢。

------解决方案--------------------
replace不能只替换文件中部分内容,你的这个replace应该不是windows下的

replace %%i tmp %oldvalue% %newvalue%
这里 %%i是循环中的文件名, tmp为一个临时文件。 我理解是把%%i这个文件中的 oldvalue替换为newvalue并保存在tmp中
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
你应该在之前用echo分别显示这些变量的值到底是什么,以便来组合整个replace语句到底是什么内容,而不是猜想
------解决方案--------------------
Windows系统自带的replace命令时用来替换文件的,而不是替换文件内的字符串。
直接说你的完整需求吧,我可以帮你写代码。

d:\>replace /?
Replaces files.

REPLACE [drive1:][path1]filename [drive2:][path2] [/A] [/P] [/R] [/W]
REPLACE [drive1:][path1]filename [drive2:][path2] [/P] [/R] [/S] [/W] [/U]

  [drive1:][path1]filename Specifies the source file or files.
  [drive2:][path2]         Specifies the directory where files are to be
                           replaced.
  /A                       Adds new files to destination directory. Cannot
                           use with /S or /U switches.
  /P                       Prompts for confirmation before replacing a file or
                           adding a source file.
  /R                       Replaces read-only files as well as unprotected
                           files.
  /S                       Replaces files in all subdirectories of the
&