找到一个词,代之以批处理文件一行 [英] find a word and replace a line with batch file

查看:189
本文介绍了找到一个词,代之以批处理文件一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个本地服务器上我的Windows XP虚拟框。

I have a local server on my Windows XP Virtual Box.

每当我的主网的变化就像从无线到有线或我移动到不同的网络IP我的Win XP的箱变。

Every time my main network changes like from wifi to cable or I move to a different network the IP of my Win XP box changes.

我要运行一个批处理文件到新的IP映射到某个共同的名字在我的hosts文件

I want to run a batch file to map the new IP to some common name in my hosts file

C:\\ WINDOWS \\ SYSTEM32 \\ DRIVERS \\ ETC \\主机

例如,如果我现在有:

10.97.100.74 www.myAppServer.com

和如果IP修改 192.168.11.9
我想搜索的hosts文件字符串 myAppServer 和替换

and if the ip changes to 192.168.11.9, I want to search for the string myAppServer in the hosts file and replace the whole line with

192.168.11.9 www.myAppServer.com

我是能够得到使用当前IP:

I was able to get the current IP using:

for /f "skip=1 delims={}, " %%A in ('wmic nicconfig get ipaddress') do for /f "tokens=1" %%B in ("%%~A") do set "IP=%%~B"

set IP=%IP%

但我怎么找到该行并替换?

but how do I find that line and replace it?

推荐答案

我发现这个在 DOS提示

BatchSubstitute - 通过在线分析文件线取代了子

@echo off
REM -- Prepare the Command Processor --
SETLOCAL ENABLEEXTENSIONS
SETLOCAL DISABLEDELAYEDEXPANSION

::BatchSubstitute - parses a File line by line and replaces a substring
::syntax: BatchSubstitute.bat OldStr NewStr File
::          OldStr [in] - string to be replaced
::          NewStr [in] - string to replace with
::          File   [in] - file to be parsed
::$changed 20100115
::$source http://www.dostips.com
if "%~1"=="" findstr "^::" "%~f0"&GOTO:EOF
for /f "tokens=1,* delims=]" %%A in ('"type %3|find /n /v """') do (
    set "line=%%B"
    if defined line (
        call set "line=echo.%%line:%~1=%~2%%"
        for /f "delims=" %%X in ('"echo."%%line%%""') do %%~X
    ) ELSE echo.
)

这篇关于找到一个词,代之以批处理文件一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆