2015-07-11 41 views
1

我正在尝试做的事情是加速批处理文件的读取结果。在批处理脚本中加速读取结果

我试图使用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. 
+0

尝试使用'start/b“%〜0”actionXX'并行执行每个'netsh'块,在bat文件的起始处添加'if not'%1“==”“goto%1', :action1'等标签的块。要发出每个部分的完成信号,您可以创建一个临时文件,与'netsh'相比,它是快速的。 – wOxxOm

回答

0

接下来的做法可能会快一点(没有临时文件(S),更新没有多重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命令输出似乎有点容易,因为动词与/value开关一起使用,因为它的定义良好且结构良好。你可以在这里找到所有的信息从netsh:下面的代码片段应该阅读并做好信息公开一个巨大的范围约所有启用网卡适配器(S)在定义的本地或远程计算机:

@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循环来解析的。

+0

谢谢你的建议约瑟夫。我尝试了两种方法,但实际上它们比我目前正在使用的方法慢得多。 – Panikosagros