1

I've seen it done before on someone else's Batch program, so I'm sure it's possible. Every time I launch my program I run a series of lines of code such as:

set/a num=0

set/a tog=1

set/a ran=%random% %%10 +1

However, I would like for certain variables to stay around, even after the program is closed. I need scripts that can:

  1. Write in a separate document the values of certain variables
  2. Check that document for values at any point in any usage session

Thanks in advance!

tjbushra
  • 91
  • 1
  • 8
  • Duplicate of http://stackoverflow.com/questions/5898131/set-a-persistent-environment-variable-from-cmd-exe – AVee Jun 28 '15 at 20:50
  • @AVee I read that question and it doesn't seem to answer my request. That or I'm not good enough at Batch to understand – tjbushra Jun 28 '15 at 21:03
  • Also worth noting that as long as the command prompt that the script runs in stays open, the variable will also be available. So instead of double-clicking the script, open the command prompt, `cd` to wherever it is, and then run your script from there and just never close the command prompt. – SomethingDark Jun 28 '15 at 21:05
  • And look at the answer under the accepted answer in the other question. As long as you don't need `/a` or `/p` (and you only need `/a` when you're doing math with variables, not when you're setting variables to numbers), then you can just use `setx` instead of `set`. – SomethingDark Jun 28 '15 at 21:09
  • @SomethingDark I need the make a folder of files that I can download onto someone else's computer and it will work without any instructions. I want it to be user friendly enough that they can just run a .bat and not have to worry about anything. I just need a permanent memory file so that when they turn off sound effects or turn off animations, it will remember their preferences next time they run the .bat – tjbushra Jun 28 '15 at 21:11
  • Yes, `setx` will do that. Although it sounds like you might want to just make a config file instead. – SomethingDark Jun 28 '15 at 21:13
  • Either you want to set a variable permanently, in which case the answer is in the link (use setX), or you want to save something in a file. In that case you should search how to read and write files in a batch script. There are plenty of answers about that as well. – AVee Jun 28 '15 at 21:14
  • @SomethingDark what's a config file? You'll have to forgive me, I had a pretty scattered teaching of batch from a lot of unqualified teachers. – tjbushra Jun 28 '15 at 21:15
  • It's a text file that stores settings. You may have seen them before as .ini files. The vast majority of programs (not just batch scripts, but literally **all** programs) use them. – SomethingDark Jun 28 '15 at 21:16
  • @SomethingDark well that does sound like what I'm looking for. How would I implement that into my project? – tjbushra Jun 28 '15 at 21:18

1 Answers1

3

You need a save system like so:

(
  echo %VARIABLE1%
  echo %VARIABLE2%
  echo %VARIABLE3%
  echo %VARIABLE4%
  echo %VARIABLE5%
) > LOGS.prgmsav

The file extension can be .txt or something weird like .nerfguns, to load LOGS.prgmsav:

< LOGS.prgmsav (
  set /p VARIABLE1=
  set /p VARIABLE2=
  set /p VARIABLE3=
  set /p VARIABLE4=
  set /p VARIABLE5=
)

The VARIABLE is any variable, you can save as many as you want! I hope I helped!

SomethingDark
  • 13,229
  • 5
  • 50
  • 55
ender_scythe
  • 470
  • 7
  • 16