欢迎来到天天文库
浏览记录
ID:57725334
大小:14.00 KB
页数:2页
时间:2020-09-02
《写了个批量替换字符串的批处理.doc》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、写了个批量替换字符串的批处理(replaceChar.bat)在windows上跑case时,我用它来替换xml文档中的名称和值什么的,还可以。但如果遇到bat中有特殊意义的字符比如<,>,%等就不行了,而且替换过程会消除每行的空格。鉴于dos知识有限,还没找到好的办法来避免这两种状况。如果用shell脚本完成替换功能就简单多了。或者直接用ultraedit的替换功能更简单。code如下:@echooffrem***************************************************************************rem**replaceChar
2、.bat-commandlineargumentsList **rem** **rem**%1 -ext_name **rem**%2 -charactersreplaced **rem**%3 -newcharacters **rem
3、** **rem***************************************************************************setext_name=%1setold_char=%2setnew_char=%3if"%ext_name%"==""gotowarningif"%old_char%"==""gotowarningif"%new_char%"==""gotowarningsetlocalenabledel
4、ayedexpansionset/aa=1for%%xin(*.%ext_name%)do( for/f"tokens=*"%%iin(%%x)do( set"var=%%i" ifnot!var!.==.( set"var=!var:%old_char%=%new_char%!" echo!var!!>>!a!.xml ) )set/aa=!a!+1)gotoend:warningechothetoolreplaceCharrequires3commandlinearguments:echo1)extnameoffileinwhichyouwanttorep
5、lacesomecharactersecho2)charactersyouwanttobereplacedecho3)newcharacterswithwhichyouwanttoreplacesomecharaceters:endecho*****endtoreplacecharacters*****echo-如果想修改后的文件名和原来的一样,可以把这一段“echo!var!!>>!a!.xml”改成“echo!var!!>>[path]%%x.xml”,其中[path]是你的磁盘路径。
此文档下载收益归作者所有