1.

Solve : Find a value in column 1 of a txt file and assign value in column 2 to a var?

Answer»

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?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



Discussion

No Comment Found