find a word and replace a line with batch file

心已入冬 提交于 2019-12-13 20:58:05

问题


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

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.

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\hosts

for example if I have now:

10.97.100.74 www.myAppServer.com

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

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?


回答1:


I found this on DOS Tips

BatchSubstitute - parses a File line by line and replaces a substring

@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.
)



回答2:


If you want to replace just one line, you can do it this way:

@echo off
setlocal

cd "C:\WINDOWS\system32\drivers\etc"
for /F "delims=:" %%a in ('findstr /N "myAppServer" hosts') do set lineNum=%%a
(for /F "tokens=1* delims=:" %%a in ('findstr /N "^" hosts') do (
   if %%a equ %lineNum% (
      echo 192.168.11.9 www.myAppServer.com
   ) else (
      echo %%a
   )
)) > hosts.new

This program eliminate empty lines and will have problems if the hosts file contain Batch special characters, like | > < & !. These details may be fixed, if needed.




回答3:


The simplest way of doing it is not replacing it. Remove it and add a new line.

If you have your ip address in %IP%, then

set "hostsFile=%systemroot%\system32\drivers\etc\hosts"
(
    findstr /v /i /c:"www.MyAppServer.com" "%hostsFile%"
    echo(
    echo %IP% www.MyAppServer.com
) > "%temp%\hosts"

move /y "%temp%\hosts" "%hostsFile%" >nul 2>nul

And be sure you have rights enough to change the hosts file and that no antivirus is blocking changes to it.




回答4:


this get the job done finally:

@echo off
REM Set a variable for the Windows hosts file location
set "hostpath=%systemroot%\system32\drivers\etc"
REM set "hostpath=C:\projectStar\Programs\etc" 
set "hostfile=hosts"

REM Make the hosts file writable
attrib -r -s -h "%hostpath%\%hostfile%"

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

move /y "%hostpath%\%hostfile%" "Hs.txt"
findstr /v/c:"www.nexsoftnetwork.com" Hs.txt > "Hss2.txt"
echo %IP%   www.nexsoftnetwork.com >>"Hss2.txt"

move /y "Hss2.txt" "%hostpath%\%hostfile%"

echo Done.
pause

thank you all for your inputs. But I wanted to hit this server from my phone which will be on the same network as the server. and eventually I came to notice that unless I modify the hosts file of the host operating system or the mobile phone it won't still solve my problem. this solution works only if I want to hit this server with in the virtual box.



来源:https://stackoverflow.com/questions/20339360/find-a-word-and-replace-a-line-with-batch-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!