Does reg query work?
- set a user env var: (your app etc)
- get it with reg query and put into a variable locally for processing :
*for /f "tokens=3" %f in ('reg query HKEY_CURRENT_USER\Environment /v
someVar') do @set someVar=%f*
I set the variable after the command prompt has been started.
C:\Users\preet>for /f "tokens=3" %f in ('reg query
HKEY_CURRENT_USER\Environment /v someVar') do @set someVar=%f
C:\Users\preet>echo %someVar%
someValueXXX
C:\Users\preet>
Preet
On 23 October 2011 14:48, Greg Keogh <[email protected]> wrote:
> Several years ago I tried to find a way of setting (creating) DOS
> environment variables in code and then using the values in batch files. I
> remember I failed because the variables only existed for the duration of the
> process. I had a fresh look at this problem today and was thrilled to see a
> 3rd argument added to the
> SetEnvironmentVariable<http://msdn.microsoft.com/en-us/library/system.environment.setenvironmentvariable.aspx>method.
> I wrote a console app with this single line of code:
> ****
>
> ** **
>
> Environment.SetEnvironmentVariable("Foo", string.Format("{0:HHmmss}",
> DateTime.Now), EnvironmentVariableTarget.User);****
>
> ** **
>
> In a bat file I then called this app followed by a "set f" command. But
> sadly the Foo value is not present. I tried setting target to Machine, but
> this requires admin privileges to write to HKLM. Even more sadly it does
> work either because it seems to write the reg entry but the new value is not
> immediately picked up by the batch file.****
>
> ** **
>
> Does anyone know of a trick to programmatically set environment variables
> for use in a bat file?****
>
> ** **
>
> Greg****
>
> ** **
>
> ** **
>
--
regards,
Preet, Overlooking the Ocean, Auckland