[Batch] The Official Batch File Repository - Learn & ask questions about batch files

Discussion in 'Scripting' started by timesurfer, Oct 11, 2010.

  1. wk-952

    wk-952 MDL Member

    Sep 2, 2012
    116
    286
    10
    #361 wk-952, Sep 18, 2014
    Last edited by a moderator: Apr 20, 2017
    @xinso

    i found a solution but it's not working correctly as the last character returned from the WMIC command is a carriage return (<CR>) character,
    give me some more time i'll try to fix this.

    (this isn't working but the output is correct and similar to what you want)
    Code:
    for /f "skip=1 tokens=1,2 delims= " %%A in ('"wmic nic where (NetConnectionID='Tunngle') get NetConnectionID,PNPDeviceID /format:texttablewsys"') do (
    echo "[%%~A]"
    echo "{%%~B}"
    )
    
    output:
    (see how the last 2 lines are completely weird!, they should be empty)
    Code:
    "[Tunngle]"
    "{ROOT\NET\0000}"
    ]"
    "{}"
    
    EDIT:
    ok try this one and provide the output, i'll try to find a cleaner solution:
    Code:
    for /f "skip=1 tokens=1,2 delims= " %%A in ('"wmic nic where %IX% get NetConnectionID, PNPDeviceID /format:texttablewsys"') do (
        set "first=%%~A"
        set "second=%%~B"
        goto :outOFloop
    )
    :outOFloop
    
    
    echo:
    echo "%first%"
    echo "%second%"
    echo:
    
    EDIT 2:
    the red colored syntax is critical and shouldn't be skipped/abandoned
     
  2. xinso

    xinso MDL Guru

    Mar 5, 2009
    5,093
    7,170
    180
    #362 xinso, Sep 18, 2014
    Last edited by a moderator: Apr 20, 2017
    OK. I'll test it. And here is what I found:

    wmic nic where ServiceName='tap0901' get Index
    pause
    Code:
    Index
    0
    wmic nic where Index=0 get NetConnectionID, PNPDeviceID /%FL%
    pause
    Code:
    NetConnectionID=TAP-9.0.2.1
    PNPDeviceID=ROOT\NET\0000
    for /f "tokens=2 delims==" %%A in ('"wmic nic where Index=0 get NetConnectionID, PNPDeviceID /%FL%"') do (echo %%A)
    pause
    Code:
    TAP-9.0.2.1
    ROOT\NET\0000
     
  3. xinso

    xinso MDL Guru

    Mar 5, 2009
    5,093
    7,170
    180
    #363 xinso, Sep 18, 2014
    Last edited by a moderator: Apr 20, 2017
    Wk-952, one more step to success, please:

    Code:
    :NO
    :--------------------------------------
    wmic nic get Index /%FL% | findstr /i Index > "%temp%\Be.txt"
    if defined Y (call:DT "%RT%\%DIR%\%INF%") else (call:CT "%KQ%\%OA%\OemVista.inf")
    wmic nic get Index /%FL% | findstr /i Index > "%temp%\Af.txt"
    for /f "delims=" %%A in ('findstr /v /g:"%temp%\Be.txt" "%temp%\Af.txt"') do (set IX=%%A)
    :--------------------------------------
    echo %IX%
    pause
    
    wmic nic where %IX% get NetConnectionID, PNPDeviceID /format:texttablewsys
    pause
    
    for /f "skip=1 tokens=1,2 delims= " %%A in ('"wmic nic where %IX% get NetConnectionID, PNPDeviceID /format:texttablewsys"') do (
        set "first=%%~A"
        set "second=%%~B"
        goto :outOFloop
    )
    
    :outOFloop
    echo:
    echo "%first%"
    echo "%second%"
    pause
    Code:
    Index=0
    Press any key to continue . . .
    
    NetConnectionID  PNPDeviceID
    乙太網路 2       ROOT\NET\0000
    Press any key to continue . . .
    
    "乙太網路"
    "2"
    Press any key to continue . . .
    
    If NetConnectionID=Local Area Connection 2, then the result would be

    "Local"
    "Area"

    We want "Local Area Connection 2" and "ROOT\NET\0000".
     
  4. wk-952

    wk-952 MDL Member

    Sep 2, 2012
    116
    286
    10
    #364 wk-952, Sep 18, 2014
    Last edited by a moderator: Apr 20, 2017
    @xinso

    try this one:
    Code:
    for /f "tokens=2,3 delims=," %%A in ('"wmic nic where %IX% get NetConnectionID, PNPDeviceID /format:csv"') do (
        set "first=%%~A"
        set "second=%%~B"
    )
    
    
    echo "%first%"
    echo "%second%"
    
     
  5. xinso

    xinso MDL Guru

    Mar 5, 2009
    5,093
    7,170
    180
    #365 xinso, Sep 18, 2014
    Last edited by a moderator: Apr 20, 2017
    The Value is correct, and install a TAP, but did not turn it to TAP-9.0.2.1
    Code:
    Index=0
    Press any key to continue . . .
    
    NetConnectionID  PNPDeviceID
    乙太網路 2       ROOT\NET\0000
    Press any key to continue . . .
    
    乙太網路 2
    ROOT\NET\0000
    Press any key to continue . . .
    
    TAP-9.0.2.1 install did not complete successfully.
    Press any key to continue . . .
     
  6. wk-952

    wk-952 MDL Member

    Sep 2, 2012
    116
    286
    10
    #366 wk-952, Sep 18, 2014
    Last edited: Sep 18, 2014
    @xinso

    could you please provide the commands you used so i can see or figure out what's the problem ?
     
  7. xinso

    xinso MDL Guru

    Mar 5, 2009
    5,093
    7,170
    180
    #367 xinso, Sep 18, 2014
    Last edited by a moderator: Apr 20, 2017
    Yes, Wk-952.

    Code:
    :NO
    :--------------------------------------
    wmic nic get Index /%FL% | findstr /i Index > "%temp%\Be.txt"
    if defined Y (call:DT "%RT%\%DIR%\%INF%") else (call:CT "%KQ%\%OA%\OemVista.inf")
    wmic nic get Index /%FL% | findstr /i Index > "%temp%\Af.txt"
    for /f "delims=" %%A in ('findstr /v /g:"%temp%\Be.txt" "%temp%\Af.txt"') do (set IX=%%A)
    :--------------------------------------
    echo %IX%
    pause
    wmic nic where %IX% get NetConnectionID, PNPDeviceID /format:texttablewsys
    pause
    
    
    for /f "tokens=2,3 delims=," %%A in ('"wmic nic where %IX% get NetConnectionID, PNPDeviceID /format:csv"') do (
      set "CI=%%~A"
      set "DI=%%~B"
    )
    echo %CI%
    echo %DI%
    pause
    echo:netsh interface set interface name="%CI%" newname="%TP%" >nul 2>&1
    "%KQ%\%OA%\devcon" sethwid @%DI% := =%TP% >nul 2>&1
    goto:eof
    Edit: Wk-952, It seems that I made a mistake

    Code:
    echo:netsh interface set interface name="%CI%" newname="%TP%" >nul 2>&1
    Should be

    Code:
    netsh interface set interface name="%CI%" newname="%TP%" >nul 2>&1
    Edit: Yes. It is working now

    Code:
    TAP-9.0.2.1 has been successfully installed with V9.00.00.9.
    Code:
    Activating Windows(R), Enterprise edition (81671aaf-79d1-4eb1-b004-8cbbe173afea) ...
    Product activated successfully.
    
    
    Name: Windows(R), Enterprise edition
    Description: Windows(R) Operating System, VOLUME_KMSCLIENT channel
    Partial Product Key: MKKG7
    License Status: Licensed
    Volume activation expiration: 259200 minute(s) (180 day(s))
    Configured Activation Type: KMS
    
    Most recent activation information:
    Key Management Service client information
        Client Machine ID (CMID): 572f8c9c-7819-4bf1-bacc-ba5d11d38c36
        Registered KMS machine name: 9.0.2.0:1688
        KMS machine IP address: 9.0.2.0
        KMS machine extended PID: 06401-00206-271-158280-03-1028-9600.0000-2592014
        Activation interval: 120 minutes
        Renewal interval: 10080 minutes
        KMS host caching is enabled
    Code:
    ROOT\NET\0002                                               : Removed
    1 device(s) were removed.
    
    TAP-9.0.2.1 has been uninstalled successfully.
    You've done it again, my dear friend.
     
  8. xinso

    xinso MDL Guru

    Mar 5, 2009
    5,093
    7,170
    180
    #368 xinso, Sep 18, 2014
    Last edited by a moderator: Apr 20, 2017
    You've done it again, my dear friend.

    Now I can loudly call it "Final". Yeah. Thanks a lot.

    Edit: But I used it as per

    Code:
    for /f "tokens=2,3 delims=," %%A in ('"wmic nic where %IX% get NetConnectionID, PNPDeviceID /format:csv"') do (set CI=%%A&set DI=%%B)


    Will it be Alright?
     
  9. wk-952

    wk-952 MDL Member

    Sep 2, 2012
    116
    286
    10
    #369 wk-952, Sep 18, 2014
    Last edited by a moderator: Apr 20, 2017
    yes but wrap the variable and its value in quotes like this:

    Code:
    for /f "tokens=2,3 delims=," %%A in ('"wmic nic where %IX% get NetConnectionID, PNPDeviceID /format:csv"') do (set "CI=%%A"&set "DI=%%B")
    
    just in case the value contains spaces or special characters, and don't worry those wraping quotes won't be appended to the variable
     
  10. xinso

    xinso MDL Guru

    Mar 5, 2009
    5,093
    7,170
    180
    #370 xinso, Sep 18, 2014
    Last edited by a moderator: Apr 20, 2017
    I see. And what if I already had quotes for the next command? Is it still necessary to quote the Variable?

    Code:
    netsh interface set interface name="%CI%" newname="%TP%" >nul 2>&1
     
  11. wk-952

    wk-952 MDL Member

    Sep 2, 2012
    116
    286
    10
    #371 wk-952, Sep 18, 2014
    Last edited by a moderator: Apr 20, 2017
    yep, the wrapping quotes in the set command are necessary, as they make sure that spaces and special characters do not crash the for loop and
    also as i told you they do not affect the variable contents (they're not appended).

    The quotes that you used in the next command is an excellent coding practice, i recommend keeping the quotes in the set command and also in this next command (and probably the other next commands).
     
  12. xinso

    xinso MDL Guru

    Mar 5, 2009
    5,093
    7,170
    180
    #372 xinso, Sep 18, 2014
    Last edited by a moderator: Apr 20, 2017
    Yes, Wk-952,

    I'd renew as

    Code:
    for /f "tokens=2,3 delims=," %%A in ('"wmic nic where %IX% get NetConnectionID, PNPDeviceID /format:csv | findstr /i ROOT"') do (set "CI=%%A"&set "DI=%%B")
    
    netsh interface set interface name="%CI%" newname="%TP%" >nul 2>&1
    
    "%KQ%\%OA%\devcon" sethwid @%DI% := =%TP% >nul 2>&1
    Thanks, mate.

    Edit: I've renewed as

    Code:
    :YES
    :--------------------------------------
    :Thanks to Wk-952's instruction
    for /f "tokens=7 delims=\ " %%A in ('reg query ^^^"%PH%\^^%%SystemRoot^^%%/system32/DRIVERS/tap0901.sys^^^" /v Source') do %E0% (set DIR=%%A)
    for /f "delims=" %%A in ('"dir /b %RT%\%DIR% 2^>nul | findstr /i inf"') do %E0% (set INF=%%A)
    if defined INF (if exist "%RT%\%DIR%\tap0901.sys" set Y=1) else (call:NF)
    
    :NO
    :--------------------------------------
    wmic nic get Index /%FL% | findstr /i Index > "%temp%\Be.txt"
    if defined Y (call:DT "%RT%\%DIR%\%INF%") else (call:CT "%KQ%\%OA%\OemVista.inf")
    wmic nic get Index /%FL% | findstr /i Index > "%temp%\Af.txt"
    for /f "delims=" %%A in ('findstr /v /g:"%temp%\Be.txt" "%temp%\Af.txt"') do (set IX=%%A)
    :--------------------------------------
    :Thanks to Wk-952's instruction
    for /f "tokens=2,3 delims=," %%A in ('"wmic nic where %IX% get NetConnectionID, PNPDeviceID /format:csv | findstr /i ROOT"') do (set "CI=%%A"&set "DI=%%B")
    netsh interface set interface name="%CI%" newname="%TP%" >nul 2>&1
    "%KQ%\%OA%\devcon" sethwid @%DI% := =%TP% >nul 2>&1
    goto:eof
     
  13. xinso

    xinso MDL Guru

    Mar 5, 2009
    5,093
    7,170
    180
    #373 xinso, Sep 18, 2014
    Last edited by a moderator: Apr 20, 2017
    @ Wk-952

    I have to quit on this
    Code:
    :NO
    :--------------------------------------
    wmic nic get Index /%FL% | findstr /i Index > "%temp%\Be.txt"
    if defined Y (call:DT "%RT%\%DIR%\%INF%") else (call:CT "%KQ%\%OA%\OemVista.inf")
    wmic nic get Index /%FL% | findstr /i Index > "%temp%\Af.txt"
    for /f "delims=" %%A in ('findstr /v /g:"%temp%\Be.txt" "%temp%\Af.txt"') do (set IX=%%A)
     :--------------------------------------
    for (the sake of "echo was off" resulting in %IX% (Index) was not revealed)
    Code:
    NetConnectionID - Invalid alias verb.
    
    TAP-9.0.2.1 install did not complete successfully.
    Note: This works while there are few TAPs.




    And now I managed this way
    Code:
    :TAP
    :--------------------------------------
    "%KQ%\%OA%\devcon" hwids @ROOT\NET\* 2>nul | findstr /i %TP% >nul&&%E0% (goto:FINISH)
    :--------------------------------------
    wmic nic get ServiceName 2>nul | findstr /i tap0901 >nul 2>&1
    if %errorlevel% EQU 0 (call:YES) else (set N=1&call:NO)
    Code:
    :NO
    :--------------------------------------
    if defined N (echo 0 > "%temp%\Be.txt") else (wmic nic where ServiceName='tap0901' get Index /%FL% 2>nul | findstr /i Index > "%temp%\Be.txt")
    
    if defined Y (call:DT "%RT%\%DIR%\%INF%") else (call:CT "%KQ%\%OA%\OemVista.inf")
    
    wmic nic where ServiceName='tap0901' get Index /%FL% 2>nul | findstr /i Index > "%temp%\Af.txt"
    
    for /f "delims=" %%A in ('findstr /v /g:"%temp%\Be.txt" "%temp%\Af.txt"') do (set IX=%%A)
    Code:
    TAP-9.0.2.1 has been successfully installed with V9.00.00.9.

    Regards
     
  14. wk-952

    wk-952 MDL Member

    Sep 2, 2012
    116
    286
    10
    #374 wk-952, Sep 18, 2014
    Last edited by a moderator: Apr 20, 2017
    @xinso

    after which command you got this error also if possible please turn on echo before this command:
    Code:
    NetConnectionID - Invalid alias verb.
    
    
    TAP-9.0.2.1 install did not complete successfully.
    
    i'm very sorry i didn't get you properly, but where is the issue in this code it seems correct in terms of syntax,
    so why are you getting the previously mentioned error?:confused:
     
  15. xinso

    xinso MDL Guru

    Mar 5, 2009
    5,093
    7,170
    180
    #375 xinso, Sep 18, 2014
    Last edited by a moderator: Apr 20, 2017
    I have no idea about this.

    One TAP, OK. Two TAPs, OK. But crashed when there were some ten TAPs.

    By this time, the %IX% was unable to be shown (since echo has been off).

    Turn echo on, yet to no avail.

    Thank you so much for helping me a lot. I've been more than happy with your kind and efficient instructions.
     
  16. wk-952

    wk-952 MDL Member

    Sep 2, 2012
    116
    286
    10
    #376 wk-952, Sep 18, 2014
    Last edited by a moderator: Apr 20, 2017
    this means that the for loop responsible for creating the variable %IX% was not executed properly (executed with errors),
    Code:
    :NO
    ...
    for /f "delims=" %%A in ('findstr /v /g:"%temp%\Be.txt" "%temp%\Af.txt"') do (set IX=%%A)
    could you please reproduce the error (install many ten TAPs) and provide a full output of the 2 files Be.txt and Af.txt ?

    EDIT:
    very sorry, i can't do that since i have very limited knowledge in programming languages.

    EDIT 2:
    please also make sure echo is turned on for the part/sub-routine :)NO) that has the problem.
     
  17. xinso

    xinso MDL Guru

    Mar 5, 2009
    5,093
    7,170
    180
    #377 xinso, Sep 18, 2014
    Last edited by a moderator: Apr 20, 2017
    Yes. Sure. Thank you!

    Please wait few Secs.

    Here it goes
    Code:
    :NO
    :--------------------------------------
    wmic nic get Index /%FL% | findstr /i Index > "%temp%\Be.txt"
    
    
    type "%temp%\Be.txt"
    pause
    
    
    if defined Y (call:DT "%RT%\%DIR%\%INF%") else (call:CT "%KQ%\%OA%\OemVista.inf")
    wmic nic get Index /%FL% | findstr /i Index > "%temp%\Af.txt"
    
    
    type "%temp%\Af.txt"
    pause
    
    
    for /f "delims=" %%A in ('findstr /v /g:"%temp%\Be.txt" "%temp%\Af.txt"') do (set IX=%%A)
    
    echo %IX%
    pause
    Code:
    Index=0
    Index=1
    Index=2
    Index=3
    Index=4
    Index=5
    Index=6
    Index=7
    Index=8
    Index=9
    Index=10
    Index=11
    Index=12
    Index=13
    Index=14
    Index=15
    Press any key to continue . . .
    Index=0
    Index=1
    Index=2
    Index=3
    Index=4
    Index=5
    Index=6
    Index=7
    Index=8
    Index=9
    Index=10
    Index=11
    Index=12
    Index=13
    Index=14
    Index=15
    Index=16
    Press any key to continue . . .
    ECHO is off.
    Press any key to continue . . .
    NetConnectionID - Invalid alias verb.
    
    TAP-9.0.2.1 install did not complete successfully.
    
    Press any key to continue . . .
     
  18. wk-952

    wk-952 MDL Member

    Sep 2, 2012
    116
    286
    10
    #378 wk-952, Sep 18, 2014
    Last edited: Sep 18, 2014
  19. xinso

    xinso MDL Guru

    Mar 5, 2009
    5,093
    7,170
    180
    #379 xinso, Sep 18, 2014
    Last edited by a moderator: Apr 20, 2017
    Neither do I. But we can do it well following instructions by the Master. Would you mind having a go for it?
     
  20. xinso

    xinso MDL Guru

    Mar 5, 2009
    5,093
    7,170
    180
    No worries. Please take your time. Much obliged, Wk-952.