Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Find a value in column 1 of a txt file and assign value in column 2 to a var  (Read 27662 times)

0 Members and 1 Guest are viewing this topic.

contrid

    Topic Starter


    Newbie

    • Experience: Familiar
    • OS: Windows 10
    I have GETPCName.txt that contains two columns
    007321182553 SP-ContriD
    CNU2469BBG 6470B-194880
    CNU2469B72 6470B-203019

    I run the following batch file on a computer with s/n 007321182553 and it results in "There is hope!"

    for /f "tokens=2 delims==" %%f in ('wmic bios get serialnumber /value ^| find "="') do set "PCSN=%%f"
    echo %PCSN%

    findstr /m "%PCSN%" GetPCName.txt
    if %errorlevel%==0 (
    echo There is hope!
    )


    What I want to after finding that string is assign "SP-ContriD" on that line to a variable that I can use in the rest of the batch file.  How do I do that?

    Sidewinder



      Guru

      Thanked: 139
    • Experience: Familiar
    • OS: Windows 10
    This may help. A new variable (NewVar) is created with a value of SP-ContriD. The WMIC output was perfectly setup, only needed a set instruction to precede it.

    Code: [Select]
    @echo off

    for /f "tokens=1" %%f in ('wmic bios get serialnumber /value ^| find /i "serialnumber"') do set %%f

    for /f "tokens=1-2" %%i in (GetPCName.txt) do (
      if %%i==%SerialNumber% set NewVar=%%j
    )

    if %errorlevel%==0 (
      echo There is hope!
    )

    echo Variable NewVar has a value of %NewVar%

    Happy Computing
    The true sign of intelligence is not knowledge but imagination.

    -- Albert Einstein