using FOR /F to query registry

April 13, 2011 at 13:48:24
Specs: Windows XP
Hello All
I am trying to get and assign a registry value to a variable.
The script starting at GETLOCATION is below. There is a tab after delims=. Also an example of what the output is as follows:
1-1018 Net Team, Joe J User x1234
What I want is just up to the first space: 1-1018

:GETLOCATION
Echo GETTING LOCATION
FOR /F "tokens=2* delims= " %%A IN ('REG QUERY "HKLM\Software\CompanyName\Workstation\Inventory\Workstation" /v "Location"') DO SET Location=%%B
Echo %Location%
Echo.
pause


See More: using FOR /F to query registry

Report •


#1
April 13, 2011 at 15:02:44
instead of using B* as token use * and then echo %a and %b and see if works..

Subhash Chandra.


Report •

#2
April 13, 2011 at 15:47:13
Could you spell that out my friend as I am not quite understanding the change you are suggesting. Just spell out the For statement as you would suggest I rewrite it plz. TY

Report •

#3
April 14, 2011 at 17:26:20
try this..
I created a reg key under HKEY_LOCAL_MACHINE\SOFTWARE\CompanyName\Workstation\Inventory\Workstation and value named location with data 1-1018 Net Team, Joe J User x1234. The batch just shows

GETTING LOCATION
1-1018
is that what you want ?

@echo off
:GETLOCATION
Echo GETTING LOCATION
FOR /F "tokens=2,3,4 delims= " %%A IN ('REG QUERY "HKLM\Software\CompanyName\Workstation\Inventory\Workstation" /v "Location"') DO set Location=%%B
Echo %Location%
Echo.
pause

Subhash Chandra.


Report •

Related Solutions

#4
April 14, 2011 at 18:29:47
That could even be stripped down a bit:


FOR /F "tokens=3" %%A IN ('
REG QUERY "HKLM\Software\CompanyName\Workstation\Inventory\Workstation" /v "Location"
') DO set Location=%%A


Report •

#5
April 16, 2011 at 20:02:16
mmm.. thats the difference between an expert and newbie :)
still in learning phase..

Subhash Chandra.


Report •


Ask Question