Jump to content

Read registry seting to set envir var


MrChris

Recommended Posts

I am looking for a way I can Read a win2000/xp registry setting and set the data value as an enviroment var that can be used in a batch file script. I have check the reg.exe command but done see any method that can have the output of just the data be a VAR.

Anyone know of a way?

Thanks a ton.

MrChris

Link to comment
Share on other sites


@ MrChris

Here is a template using reg.exe with an example added to help you understand

echo off&setlocal enableextensions

:: put the registry key you want to read after the equals symbol below
set keyname=HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run

:: put the value name of the registry key after the equals symbol below
set valuename=nwiz

for /f "tokens=1,3* delims= " %%a in ('reg query %keyname%^|findstr/i "%valuename%"') do set mynewvar="%%~b"
echo/you have set a new local variable named mynewvar as %mynewvar%
endlocal&goto :eof

Note

The space after delims= is a Tab

Link to comment
Share on other sites

@ MrChris

Here is a template using reg.exe with an example added to help you understand

echo off&setlocal enableextensions

:: put the registry key you want to read after the equals symbol below
set keyname=HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run

:: put the value name of the registry key after the equals symbol below
set valuename=nwiz

for /f "tokens=1,3* delims= " %%a in ('reg query %keyname%^|findstr/i "%valuename%"') do set mynewvar="%%~b"
echo/you have set a new local variable named mynewvar as %mynewvar%
endlocal&goto :eof

Note

The space after delims= is a Tab

Thank you very much. That is kinda of what im looking for but only in my case the key value will always be the same name but the data that belongs to that value is

what will always be diff. If that makes sense.

for example.

HKEY_CURRENT_USER\Software\EasyBoot Systems\UltraISO\5.0

has a string name=UserName but the data for that string is always diff. I want to set the data from the value as the env. var. In your above example it was setting the value itsself as the var.

I know batch scripting a little but not a lot. I only know the simple parts of it. And I know that there are a ton load of commands that can be passed in a batch file.

Thanks again for the help.

MrChris

Edited by MrChris
Link to comment
Share on other sites

Well unfortunately, your particular example may not work with the template I gave due to the space in the key name, therefore try

echo off&setlocal enableextensions

set keyname=HKCU\Software\EasyBoot Systems\UltraISO\5.0

:: put the value name of the registry key after the equals symbol below
set valuename=

for /f "tokens=1,3* delims= " %%a in ('reg query "%keyname%"^|findstr/i "%valuename%"') do set mynewvar="%%~b"
echo/Data Value %mynewvar% is now set to variable mynewvar
endlocal&goto :eof

i.e. add the quotation marks to the %keyname% in your reg query command

Link to comment
Share on other sites

As I do not have the application you are using, it is difficult for me to test, however, if you have ensured that the space is in fact a tab only, it should work; as should this shorter version

echo off&setlocal enableextensions
for /f "tokens=3* delims= " %%a in ('reg query "HKCU\Software\EasyBoot Systems\UltraISO\5.0" /v UserName') do set mynewvar="%%~a"
echo/you have set a new local variable named mynewvar as %mynewvar%
endlocal&goto :eof

making sure once again that delims=<Tab>

If it still doesn't work try posting an output of the reg query command from a cmd session:

reg query "HKCU\Software\EasyBoot Systems\UltraISO\5.0" /v UserName

You should get an output consisting of the three tokens, 1st being your value name, 2nd being the data type and the third being the data value you require, each seperated by a tab

Edited by Yzöwl
Link to comment
Share on other sites

Ahh yes this worked just as I need. Thanks a ton. But the data of the value has spaces in it and its only getting the first part of it. None the less thanks. The output is there when the batch is done running. but when I view the var with the SET command I only see the first part of the data.

I dont think this type of a task will onyl work for the app i refer too. It should work across the board.

Edited by MrChris
Link to comment
Share on other sites

Just as a side note, if you are wishing to add the variable to your system, i.e. always there; since the current variable is only local to the running batch.

@echo off&setlocal enableextensions

for /f "tokens=3* delims= " %%a in (
'reg query "HKCU\Software\EasyBoot Systems\UltraISO\5.0" /v UserName^2^>nul'
) do (
if errorlevel 0 (
reg add "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v MyNewVar /d "\"%%~a\"" /f&&echo/you have set a new environment variable named MyNewVar
)
)

endlocal&goto :eof

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...