Josh
Josh

Reputation: 70

Batch file for loop with tokens over multiple lines

I am trying to take the hotfixes output from systeminfo:

Hotfix(s):                 9 Hotfix(s) Installed.
                           [01]: KB3124262
                           [02]: KB3135173
                           [03]: KB3136561
                           [04]: KB3139907
                           [05]: KB3140741
                           [06]: KB3140743
                           [07]: KB3140768
                           [08]: KB3154132
                           [09]: KB3147458

However, I only want the KB####### part. The following:

setlocal enabledelayedexpansion 
for /F "tokens=2" %%a IN ('find "KB" hotfix.txt') do (
  set Hot=%%~a
  echo %Hot%
)

Gave me this output:

[++Hotfix(s) Installed]

KB3147458 echo KB3147458
KB3147458 echo KB3147458
KB3147458 echo KB3147458
KB3147458 echo KB3147458
KB3147458 echo KB3147458
KB3147458 echo KB3147458
KB3147458 echo KB3147458
KB3147458 echo KB3147458
KB3147458 echo KB3147458
KB3147458 echo KB3147458

Which is the same line over and over, but is the right amount of lines from what is above. Been going at this for too long and feel I am shooting around the target.

Upvotes: 3

Views: 1702

Answers (1)

npocmaka
npocmaka

Reputation: 57252

you already have enabled delayed expansion but you need to access the variabled with ! instead of %:

setlocal enabledelayedexpansion 
for /F "tokens=2" %%a IN ('find "KB" hotfix.txt') do (
  set Hot=%%~a
  echo !Hot!
)

Upvotes: 4

Related Questions