Help retrieving JUST ProductKeyID from TXT File

Discussion in 'Scripting' started by Flipp3r, Apr 29, 2014.

  1. Flipp3r

    Flipp3r MDL Expert

    Feb 11, 2009
    1,544
    662
    60
    #1 Flipp3r, Apr 29, 2014
    Last edited by a moderator: Apr 20, 2017
    Hi,

    I have log files I'd like to rename using the ProductKeyID which is found in each file. The Log file look like this:
    Code:
    <HardwareVerificationReport version="1.0">
    <Result>
    <p n="Pass">true</p>
    </Result>
    <Key inputId="1">
    <ProductKeyID>2586002108880</ProductKeyID>
    <ProductKeyState>0</ProductKeyState>
    </Key>
    </HardwareVerificationReport>
    All I need is to retrieve the value in blue. I think the fact there's <>/" characters is making it difficult to use find or findstr.

    Here's some garbage I've tried:
    Code:
    ::for /f "tokens=4 delims==;" %%a in (
    ::            'find "ProductKeyID" ^< X_LOG.txt'
    ::            ) do set "ProductKeyID=%%a"
    
    ::FINDSTR "ProductKeyID" X_LOG.txt
    
    ::del Test2.txt
    ::type X_LOG.txt | findstr /I /C:"ProductKeyID"
    
    
    FOR /F "tokens=2 delims=: " %%A in ('type X_LOG.txt ^| findstr /i "ProductKeyID"') do echo %%A
    Any help would be fantastic!
     
  2. xinso

    xinso MDL Guru

    Mar 5, 2009
    4,242
    5,511
    150
    #2 xinso, Apr 29, 2014
    Last edited by a moderator: Apr 20, 2017
    Will this do?

    Code:
    @echo off
    pushd "%~dp0"
    :--------------------------------------
    FOR /F "tokens=3 delims=<>" %%A in (' "type X_LOG.txt | findstr /i ProductKeyID" ') do (set KEY=%%A)
    echo %KEY%
    pause
    Code:
    2586002108880
    Press any key to continue . . .
     
  3. Flipp3r

    Flipp3r MDL Expert

    Feb 11, 2009
    1,544
    662
    60
    #3 Flipp3r, Apr 29, 2014
    Last edited by a moderator: Apr 20, 2017
    (OP)
    Yes, Perfect! I have some bigger logs than the sample I posted & noticed a few more instances of ProductKeyID.
    It still gave me the correct result! I'm pretty new to the Tokens command.

    This helps me out heaps. Thanks very much!:worthy:
     
  4. xinso

    xinso MDL Guru

    Mar 5, 2009
    4,242
    5,511
    150
    #4 xinso, Apr 30, 2014
    Last edited by a moderator: Apr 20, 2017
    But this syntax displays the last detected one only if there are more.

    For example,

    if you have two products installed, there should be two ProductKeyIDs like 1111111111111 and 2222222222222.

    TXT
    Code:
    <HardwareVerificationReport version="1.0">
    <Result>
    <p n="Pass">true</p>
    </Result>
    <Key inputId="1">
    <ProductKeyID>1111111111111</ProductKeyID>
    <ProductKeyState>0</ProductKeyState>
    </Key>
    
            <Result>
    <p n="Pass">true</p>
    </Result>
    <Key inputId="1">
    <ProductKeyID>2222222222222</ProductKeyID>
    <ProductKeyState>0</ProductKeyState>
    </Key>
    </HardwareVerificationReport>
    COMMAND
    Code:
    FOR /F "tokens=3 delims=<>" %%A in (' "type X_LOG.txt | findstr /i ProductKeyID" ') do (set KEY=%%A)
    echo %KEY%
    pause
    RESULT
    Code:
    2222222222222
    Press any key to continue . . .
    How to display all of them?

    COMMAND
    Code:
    @echo off
    pushd "%~dp0"
    :--------------------------------------
    FOR /F "tokens=3 delims=<>" %%A in (' "type X_LOG.txt | findstr /i ProductKeyID" ') do (echo %%A)
    pause
    RESULT
    Code:
    1111111111111
    2222222222222
    Press any key to continue . . .

    Cheers
     
  5. Flipp3r

    Flipp3r MDL Expert

    Feb 11, 2009
    1,544
    662
    60
    #5 Flipp3r, Apr 30, 2014
    Last edited by a moderator: Apr 20, 2017
    (OP)
    Thanks but I realised that last night when I changed it to:
    Code:
    FOR /F "tokens=3 delims=<>" %%A in (' "type X_LOG.txt | findstr /i ProductKeyID" ') do (echo %%A)
    Code:
    true
    2586002108880
    2586002108880
    Press any key to continue . . .
    I've checked & all the Log files I'll scan will give me the correct result. Each file will have it's own unique ID.
    Thanks again buddy!