2015-07-11 37 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