bat批处理 查找替换:批处理如何查找并替换文本里特定字符串中的部分内容
批处理如何查找并替换文本里特定字符串中的部分内容
摘自:http://www.bathome.net/thread-43349-1-1.html
脚本如下:
1 @if(0)==(0) echo off & set/p "rep=输入替换为:" 2 cscript -nologo -e:jscript "%~f0" "%rep%"<"config.ini">"new.ini"&pause&exit/b&@end 3 WSH.Echo(WScript.StdIn.ReadALL().replace(/user[0-9]+/g, WScript.Arguments(0)))
1 @if(0)==(0) set filename_will_replace=config.ini & set tmp_fimename=%filename_will_replace%_tmp@end 2 3 @if(0)==(0) echo off & set "rep=AppServer = 44x4x.444.444.444ss" 4 cscript -nologo -e:jscript "%~f0" "%rep%"<%filename_will_replace% >"%tmp_fimename%"&pause&move %tmp_fimename% %filename_will_replace%&exit/b&@end 5 6 WSH.Echo(WScript.StdIn.ReadALL().replace(/AppServer.*/g, WScript.Arguments(0)))
bat如何搜索并替换ini文件里的userx关键字。不能用第三方exe软件。
其中的x是数字,不确定位数、也不确定是哪个数字。
搜索userx关键字,然后替换成user%y%,y变量是手动输入的数字。
比如ini文件里面,这里的userx就是指user231
config.ini文件内容如下:
1 reg add "HKEY_LOCAL_MACHINE\SOFTWARE\VMware, Inc.\VMware VDM\Client" /v EnableShade /t REG_SZ /d false /f 2 reg add "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\System" /v Shell /t REG_SZ /d wscript c:\\client\\start.vbs /f 3 reg add "HKEY_LOCAL_MACHINE\SOFTWARE\VMware, Inc.\VMware VDM\Client" /v ServerURL /t REG_SZ /d vdi.aaa.bbb.cn /f 4 reg add "HKEY_LOCAL_MACHINE\SOFTWARE\VMware, Inc.\VMware VDM\Client\Security" /v CertCheckMode /t REG_SZ /d 0 /f 5 reg add "HKEY_LOCAL_MACHINE\SOFTWARE\VMware, Inc.\VMware VDM\Client" /v NonInteractive /t REG_SZ /d false /f 6 reg add "HKEY_LOCAL_MACHINE\SOFTWARE\VMware, Inc.\VMware VDM\Client" /v DomainName /t REG_SZ /d CLOUD /f 7 reg add "HKEY_LOCAL_MACHINE\SOFTWARE\VMware, Inc.\VMware VDM\Client" /v UserName /t REG_SZ /d user231 /f 8 9 ccc 10 reg add "HKEY_LOCAL_MACHINE\SOFTWARE\VMware, Inc.\VMware VDM\Client" /v Password /t REG_SZ /d ccc@123 /f 11 12 reg add "HKEY_LOCAL_MACHINE\SOFTWARE\VMware, Inc.\VMware VDM\Client" /v DesktopLayout /t REG_SZ /d FullScreen /f 13 reg add "HKEY_LOCAL_MACHINE\SOFTWARE\VMware, Inc.\VMware VDM\Client" /v connectUSBOnStartup /t REG_SZ /d true /f 14 reg add "HKEY_LOCAL_MACHINE\SOFTWARE\VMware, Inc.\VMware VDM\Client" /v connectUSBOnInsert /t REG_SZ /d true /f 15 reg add "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\System" /v connectUSBOnInsert /t REG_SZ /d true /f 16 reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System" /v disablecad /t REG_DWORD /d 1 /f 17 reg add "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\System" /v DisableTaskMgr /t REG_DWORD /d 1 /f 18 reg delete "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System" /v legalnoticecaption /f 19 reg delete "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System" /v legalnoticetext /f 20 shutdown -r -f -t 1