0

I have a really simple question:

I want to store two user-entered variable strings in the registry and be able to access it later when the batch runs again. I have been googling for a while and can't seem to find the answer.

It can be stored in any registry as long as it will be available on next execution.

Here is the flow of what needs to happen:

:: Check if variable_a/b is available in reg, else ask user....?
SET /p variable_a="Enter a string: "
SET /p variable_b="Enter a string: "

:: Store both vars somehow....?

echo My variables:
echo variable_a
echo variable_b

Can you please help me solve this simple problem?

2 Answers 2

2
setx variable_a "%variable_a%"
setx variable_b "%variable_b%"

will simply store variables - such that any future CMD.EXE invocations in the same session will load the variables/values into the environment.

Add /m to save for the next and future logon(s) (not stored for THIS logon session)

setx variable_a ""

to delete - same rules.

Sign up to request clarification or add additional context in comments.

4 Comments

So if I want to save it for this session and next logons I would have to use setx variable_a "%variable_a%" and setx variable_a "%variable_a%" \m directly after each other?
Yes - except that it's /m not \m
Oh, and can you please indicate how I can check if it exists?
if defined variable_a - what setx does is write a new value for the environment variable mentioned to the registry. Any CMD.exe(or, presumably, lesser programs) which is initiated after the setx was executed but within the same logon session will have variable_a set in its environment. The /m switch acts substantially in the same way - but on the NEXT logon session and NOT on this one.
0

basetx is not available in all windows-versions.

Also, I would prefer storing the variables in a file, not in the registry

if not exist somepath\myvar.bat (
 SET /p varA="Enter a string: " 
 SET /p varB="Enter b string: "
 echo REM this is my permanent store >somepath\myvar.bat
 echo set variable_a=%varA% >> somepath\myvar.bat
 echo set variable_b=%varB% >> somepath\myvar.bat
)
call somepath\myvar.bat

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.