Board logo

标题: 这样字符串怎么替换 [打印本页]

作者: zcyzzzm     时间: 2010-1-9 17:12    标题: 这样字符串怎么替换

@echo off
setlocal enabledelayedexpansion

set "Space=        "
set "PH_addr=%Space%Physical Address"  %'/*-----物理地址-------*/%
set "IP_addr=%Space%IP Address" %'/*------IP地址(局域网)--------*/%
for /f "tokens=1,* delims=." %%i in ('ipconfig /all') do (
   for %%a in (PH_addr IP_addr) do (
      if "%%i"=="!%%a!" set %%a=%%j
   )
)
Rem '/*===========对结果进行处理===========
set PH_addr=%PH_addr:*:=%
set IP_addr=%IP_addr:*:=%


set file="aa.txt"
for %%i in ("%file%") do set file=%%~fi
echo.
set "replaced=$cfg['Servers'][$i]['host'] = 'localhost'"
echo.
set "all=$cfg['Servers'][$i]['host'] = '%IP_addr%'"
for /f "delims=" %%i in ('type "%file%"') do (
    set str=%%i
    set "str=!str:%replaced%=%all%!"
    echo !str!>>"%file%"_tmp.txt
)
copy "%file%" "%file%"_bak.txt >nul 2>nul
move "%file%"_tmp.txt "%file%"
start "" "%file%"

我本意是替换aa.txt中的

$cfg['Servers'][$i]['host'] = 'localhost'

为:

$cfg['Servers'][$i]['host'] = '61.191.56.15'

但是替换结果变成:

'localhost'=$cfg['Servers'][$i]['host'] = '61.191.56.15'= 'localhost';

请问怎么处理,另外批处理能够获取服务器的ip吗
作者: HAT     时间: 2010-1-9 19:25
批处理能够获取服务器的ip
作者: zcyzzzm     时间: 2010-1-9 19:56


  Quote:
Originally posted by HAT at 2010-1-9 19:25:
批处理能够获取服务器的ip

for /f "tokens=15" %%i in ('ipconfig ^| find /i "ip address"') do set ip=%%i
echo %ip%

ip获取是正确的,我要替换的字符串中含有“=”号,替换出现问题了

本意是替换aa.txt中的

$cfg['Servers'][$i]['host'] = 'localhost'

为:

$cfg['Servers'][$i]['host'] = '61.191.56.15'

但是替换结果变成:

'localhost'=$cfg['Servers'][$i]['host'] = '61.191.56.15'= 'localhost';