加快批量脚本中的读取结果

时间:2015-07-11 10:19:55

标签: batch-file batch-processing netsh

我想做的是加快批量文件的阅读结果。

尝试使用netsh命令获取不同的值,然后在我的脚本控制台中显示它们,但需要很长时间。 请参阅下面我脚本的一小部分来了解这个想法。 (这只是一小部分,我实际上得到了大约50个不同的值并使用更多的netsh命令)

有人知道加速这个过程的方法吗?

.
.
.
netsh interface ipv4 show config %AdapterLAN%  >temp
for /f "tokens=3" %%i in ('findstr "IP Address" temp') do set ip=%%i
echo.  IP Address        : %ip%

for /f "tokens=5 delims=) " %%i in ('findstr  "Subnet Prefix" temp') do  set mask=%%i
echo.  Mask              : %mask%

for /f "tokens=3" %%i in ('findstr  "Gateway:" temp')  do set gateway=%%i
echo.  Gateway           : %gateway%

for /f "tokens=1,5,6" %%a in ('findstr  "DNS" temp') do set dns1=%%a&set dns5=%%b&set dns6=%%c
If "%dns1%"=="Statically" set dns=%dns5%
if "%dns1%"=="DNS"        set dns=%dns6% 
echo.  DNS Server        : %dns%

for /f "tokens=3" %%i in ('findstr "Gateway Metric" temp') do set GMetric=%%i
for /f "tokens=2" %%i in           ('findstr "InterfaceMetric" temp')   do set IMetric=%%i
set /a metricLAN=Gmetric + imetric 
echo.  Metric            : %metricLAN%

for /f "tokens=3" %%i in ('find "DHCP enabled" temp') do set LANDHCP=%%i
If "%dns1%"=="Statically"  set xx=Static
if "%dns1%"=="DNS"         set xx=DHCP  
If /i %LANDHCP%==No        set LANDHCP=Static
if /i %LANDHCP%==YES       set LANDHCP=DHCP
echo.  Obtained IP       : %LANDHCP%
echo.  Obtained DNS      : %xx%
for /f "tokens=3 delims=," %%a in ('getmac /v /fo csv ^| find """%AdapterLAN-without-Q%""" ')  do set macLAN=%%a
echo.  MAC-Addres        : %macLAN%
del temp
.
.
.
netsh wlan show profile >temp
.
Do a similar process of getting values from another netsh command sent them
in the temp file …echo the one I want on the screen ..delete the file etc.

1 个答案:

答案 0 :(得分:0)

下一步方法可能会更快一点(没有临时文件,已更新没有多重findstr):

@ECHO OFF >NUL
SETLOCAL enableextensions enabledelayedexpansion
set "AdapterLAN=wiredEthernet"
set "IMetric="
set "GMetric="
for /F "tokens=1,2* delims=:" %%G in ('
  netsh interface ipv4 show config "%AdapterLAN%"^|findstr /N /R "^"
') do (
    rem echo G="%%G" H="%%H" I="%%I"
    if "%%I"=="" (
      rem line  1   skip
      rem line  2 = Configuration for interface
      rem line 10 = DNS server #2 etc.
    ) else (
        set "hh=%%H"
        set "xx=!hh:IP Address=!"
        if not "!hh!"=="!xx!" for /F "tokens=1*" %%i in ("%%I") do set "ip=%%i"

        set "xx=!hh:Subnet Prefix=!"
        if not "!hh!"=="!xx!" for /F "tokens=3 delims=) " %%i in ("%%I") do set "mask=%%i"

        set "xx=!hh:Default Gateway=!"
        if not "!hh!"=="!xx!" for /F "tokens=1*" %%i in ("%%I") do set "gateway=%%i"

        set "xx=!hh:Gateway Metric=!"
        if not "!hh!"=="!xx!" for /F "tokens=1*" %%i in ("%%I") do set "GMetric=%%i"

        set "xx=!hh:InterfaceMetric=!"
        if not "!hh!"=="!xx!" for /F "tokens=1*" %%i in ("%%I") do set "IMetric=%%i"

    )
)
echo(  IP Address        : [%ip%]
echo(  Mask              : [%mask%]
echo(  Gateway           : [%gateway%]
set /a metricLAN=Gmetric + IMetric 
echo(  Metric            : [%metricLAN%]
ENDLOCAL
goto :eof

<强>输出

==>D:\bat\SO\31356115.bat
  IP Address        : [192.168.1.100]
  Mask              : [255.255.255.0]
  Gateway           : [192.168.1.1]
  Metric            : [20]

==>

修改

这是另一种方法:与netsh不同,当wmic动词与get切换一起使用时,解析/value命令输出似乎更容易一些&#39} ;定义明确,结构合理。您可以在此处找到netsh的所有信息:下一个代码段应该读取并公开大量有关已定义的本地或远程计算机中所有已启用的NIC适配器的信息:

@ECHO OFF >NUL
SETLOCAL enableextensions enabledelayedexpansion
set "NetCount=0"
set "compName=%computername%" :: local or remote computer name
set "compIDXs="
for /F "tokens=2 delims==" %%N in ('
  wmic /node:"%compName%" NIC where "NetEnabled=TRUE" get InterfaceIndex /value 2^>NUL ^| find "="
') do for /F "tokens=*" %%n in ("%%N") do (
  for /F "tokens=*" %%G in ('
    wmic /node:"%compName%" NIC where "InterfaceIndex=%%n" get /value 2^>NUL ^| find "="
  ') do for /F "tokens=*" %%g in ("%%G") do set "_%%n%%g"
  for /F "tokens=*" %%I in ('
    wmic /node:"%compName%" NICCONFIG where "InterfaceIndex=%%n" get /value 2^>NUL ^| find "="
  ') do for /F "tokens=*" %%i in ("%%I") do set "_%%n_%%i"
  set /A "NetCount+=1"
  set "compIDXs=!compIDXs! "%%n""
)
set _
rem sample of it:
echo compName=%compName% NetCount=%NetCount% compIDXs=%compIDXs%
for %%x in (%compIDXs%) do (
  echo enabled InterfaceIndex=%%~x  NetConnectionID=!_%%~xNetConnectionID!
  for /F "tokens=1,2 delims={}," %%i in ("!_%%~x_IPAddress!") do echo ipv4=%%~i ipv6=%%~j  
)

阅读Dave Benham的WMIC and FOR /F: A fix for the trailing <CR> problem,了解为什么通过几个嵌套的wmic循环解析任何for命令输出。

相关问题