0

我在批处理文件中尝试了此代码,但无法正常工作。我想从 DefaultGateway.txt 中取出第一行并替换 NewFile.txt 中的第三行 ip 地址。

批处理文件代码:

ipconfig /all | findstr Gateway > "C:\Program Files (x86)\""Wireless Guard\""DefaultGateway.txt"
SetLocal EnableDelayedExpansion
type nul > NewFile.txt
set "Default" =Start Line of Paragraph
set "254"=End Line which is not Included
set Flag=0
for /f "tokens=* delims=" %%a in ('type DefaultGateway.txt') do (
if /i "%StartText%" EQU "%%a" (set Flag=1) 
if /i "%EndText%" EQU "%%a" (set Flag=0) 
if !Flag! EQU 1 echo %%a >> NewFile.txt
)

默认网关.txt:

Default Gateway . . . . . . . . . : 172.20.128.254
Default Gateway . . . . . . . . . : 
Default Gateway . . . . . . . . . : 

新文件.txt:

remarks:This document is for Linksys for version v4.30.5, the auto sensing part.
"C:\Program Files (x86)\Wireless Guard\wget" --http-user= --http-password=admin 
[this is not a link]http://192.168.1.1/WClientMACList.htm
REM del WClientMACList.txt
REM del arp.txt
REM del Intruder1.txt
copy WClientMACList.htm WClientMACList.txt
del WClientMACList.htm
REM del WL_ActiveTable.asp
REM do ping for 20 seconds
REM ping -n localhos
4

1 回答 1

1

Your question/example have missed several details, so I made some assumptions. The Batch file Below:

1- Does not create the DefaultGateway.txt at all, but take the same output.

2- Read first line from that output and get the text after the colon as ipAddr.

3- Read third line of NewFile.txt and replace the text between // and / with previous ipAddr. The result is in NewFile.new file.

EDIT: I fixed a couple problems...

@echo off
setlocal EnableDelayedExpansion
REM ipconfig /all | findstr Gateway > "C:\Program Files (x86)\""Wireless Guard\""DefaultGateway.txt"
for /F "tokens=2 delims=:" %%a in (DefaultGateway.txt) do (
   set ipAddr=%%a
   goto continue
)
:continue
set num=0
(
for /F "delims=" %%a in (NewFile.txt) do (
   set "line=%%a"
   set /A num+=1
   if !num! equ 3 (
      for /F "tokens=1-3 delims=/" %%b in ("%%a") do (
         set "line=%%b//%ipAddr:~1%/%%d"
      )
   )
   echo(!line!
)
) > NewFile.new

This is the resulting NewFile when the program run with your data above:

remarks:This document is for Linksys for version v4.30.5, the auto sensing part.
"C:\Program Files (x86)\Wireless Guard\wget" --http-user= --http-password=admin 
[this is not a link]http://172.20.128.254/WClientMACList.htm
REM del WClientMACList.txt
REM del arp.txt
REM del Intruder1.txt
copy WClientMACList.htm WClientMACList.txt
del WClientMACList.htm
REM del WL_ActiveTable.asp
REM do ping for 20 seconds
REM ping -n localhos

Note that you must replace the right (full path) name of the DefaultGateway.txt file in the FOR command. If this name include a right parentheses, like (x86), you must escape it with a caret character this way: (x86^). Why not create this file in the current directory? It seems to be just an auxiliary file, right?

Another possibility is to put in this place the 'ipconfig /all | findstr' command, so the auxiliary file is entirely avoided (like I done in the first version of my program).

于 2012-05-17T18:55:42.503 回答