How can I save variables longterm in Batch? - variables

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:
Write in a separate document the values of certain variables
Check that document for values at any point in any usage session
Thanks in advance!

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!

Related

Display an asked variable in Batch (variable inside a variable)

Is there any way to ask the user which variable wants to be shown and display it? It should be, as a concept, something like this:
#echo off
set "VAR_1=THIS IS VAR_1"
set "VAR_2=THIS IS VAR_2"
set /p "VAR_TO_SHOW=Enter the variable to show: (VAR_1/VAR_2)"
echo %%VAR_TO_SHOW%%
So if we are in "echo %%VAR_TO_SHOW%%" if we entered "VAR_1" I would want to look like this:
echo %VAR_1%
So the output would be "THIS IS VAR_1". Summarizing all what I've said, I would want to make a variable inside a variable. How can I make this?
Another thing I would want to comment is that I've alredy tried "%%!VAR!%%" and "%%VAR%%" without the exclamation marks, but what is displayied is or "%!VAR_TO_SHOW!%" or the same but without (again) the exclamation marks...
#echo off
set "VAR_1=THIS IS VAR_1"
set "VAR_2=THIS IS VAR_2"
set /p "VAR_TO_SHOW=Enter the variable to show: (VAR_1/VAR_2)"
call echo %%%VAR_TO_SHOW%%%
or
#echo off
setlocal enableDelayedExpansion
set "VAR_1=THIS IS VAR_1"
set "VAR_2=THIS IS VAR_2"
set /p "VAR_TO_SHOW=Enter the variable to show: (VAR_1/VAR_2)"
echo !%VAR_TO_SHOW%!
the second way will work faster.

storing current path in variable and reusing it

I know similar questions have already been asked, but somehow I am unable to figure out the mistake in my code.
I'm making a .bat file with the following code
echo off
echo %cd%
set curr_directory = "%cd%"
echo $curr_directory
pause
OUTPUT is :
C:\Users\MyDesktop>echo off
C:\Users\MyDesktop>
$curr_directory
Press any key to continue . . .
So what I dont get is why the value of variable curr_directory is not being printed.
What i eventually want to do is use the variable to change the directory something like this: cd $curr_directory
Thanks
I don't know where to start. EVERYTHING about your code is wrong. This should work:
#echo off
echo %cd%
set curr_directory=%cd%
echo %curr_directory%
pause
In batch you access variables via %var% and not $var. Further, DO NOT PUT SPACES behind =. SET x=123 will store 123 in x but SET x= 123 will store _123 (_ means space) in x.
EDIT: As SomethingDark stated, the first line should be #echo off except you actually want the message echo off to be printed. And yes, SET x = 123 means %x % is 123
use %curr_directory% instead of $curr_directory
Avoid spaces inbetween like the below one
"set curr_directory = %cd%"
below should work
echo off
echo %cd%
set curr_directory=%cd%
echo curr_directory is %curr_directory%
pause

batch file difference between %variable% and %variable % or variable call with spaces

I've just started with batch file programming and testing variable usage momentary.
Does anyone know what is the difference between the 2 variable calls, the space before the last %
#echo off
set pathOS1="\\o1511\Pcs7ProjectO1511\OS1511\GraCS\"
ECHO We're working with %pathOS1%
ECHO We're working with %pathOS1 %
since the echo is different:
We're working with "\\o1511\Pcs7ProjectO1511\OS1511\GraCS\"
We're working with \\o1511\Pcs7ProjectO1511\OS1511\GraCS\
Delayed expansion is not enabled.
Spaces are allowed in the variable name.
set "data=100"
set "data =101"
echo %data%
echo %data %
set data
You have two variables with two similar values
So I summarize your answers:
This can happen if there's no setlocal at the start of the routine, any values set in the environment remain set, so if "pathos1 " was set previously, its value will be retained until it's cleared by a set "pathos1 =" statement or cmd is closed.
So, since I didn't close the cmd and didn't set setlocal, I called the previously set variable "pathos1 =".
It is good example for setlocal :)

How can I create a variable that is incremental in a bat file?

I am wanting to make a bat file that when I input my data, it saves a variable to another bat file. I am able to do so, but the problem is that I want to use the same variable for other data.
So is it possible to have %var%=Green and then have the bat file realize that %var% is already in use and instead uses %var1% so that it can %var1%=Red? I have tried for loops and if statements but I don't think that I am using them the right way.
Any help appreciated!
This is what I'm using right now
If Exist %var% (
set /a variable=!variable!+1)
set /p var=Enter Card Number:
set /p val=Enter Amount:
set /p fname=Enter First Name:
set /p lname=Enter Last Name:
set /p cbal=Enter Current Balance:
Edit:
so if %var% exists it should create %var1% and then if %var1% exists create %var2% and so on and so forth. Everything entered above though should be linked together. So when i type in the card number, the first name and last name along with current balance displays properly.
Your question is not well defined but this solves what you have asked:
if not defined var (
set var=green
) else (
set var1=red
)

Batch IF statement variable inside another variable

I'm having a problem with a framework for a batch game I'm trying to create. I want to check if a variable is something other than a blank space. However, which variable I'm trying to check is in itself defined by two variables. For example:
if not %px%xplayerlocation%y%yplayerlocation%%==%blank% goto wherever
As you can see, the variable to be checked is determined by the values of %xplayerlocation% and %yplayerlocation%. To my knowledge, only the outermost %% signs are being read as being a variable and the inner ones are being read as literal percent signs. Does anyone know a way around this problem? I'll give any additional information if anyone needs it. Thanks.
#ECHO OFF
SETLOCAL
SET "blank= "
SET xplayerlocation=3
SET yplayerlocation=4
ECHO test with blank================
SET px3y4=%blank%
CALL SET varval=%%px%xplayerlocation%y%yplayerlocation%%%
if not "%varval%"=="%blank%" ECHO goto wherever - NOT blank
ECHO test with "Q"================
SET px3y4=Q
CALL SET varval=%%px%xplayerlocation%y%yplayerlocation%%%
if not "%varval%"=="%blank%" ECHO goto wherever - NOT blank
GOTO :EOF
This should get you out of trouble.
You have recognized the source of the problem, but your description of the behavior is incorrect. The parser will attempt to expand variables named px and y, and it will convert the final %% into %.
The Magoo solution will work, but using CALL is quite slow. That may not be a problem for many small scripts, but for a batch game it can kill performance.
You want delayed expansion. Include setlocal enableDelayedExpansion near the beginning of your script. Then use the following:
if not !px%xplayerlocation%y%yplayerlocation%!==%blank% goto wherever
Normal %var% expansion occurs early at parse time, and !var! expansion occurs late at execution time, so you get the proper result.
The above will not work if the value of %blank% is a space. The simplest solution would be to use delayed expansion for !blank! as well.
You might find yourself in a situation where the coordinate values need to be set and expanded within the same block of code, like in a FOR loop or IF statement:
setlocal enableDelayedExpansion
...
REM This does not work
for ... in (...) do (
...
set /a "xPlayerLocation+=xChange, yPlayerLocation+=yChange"
if not !px%xplayerlocation%y%yplayerlocation%!==!blank! REM doSomething
...
)
The above will not work because %var% expansion occurs at parse time, and the entire parenthesized block of code is parsed before any code is executed. So the expanded value is constant - it will expand to the value that existed before the loop started.
The solution is to transfer the coordinate values to FOR variables using delayed expansion:
setlocal enableDelayedExpansion
...
REM This works
for ... in (...) do (
...
set /a "xPlayerLocation+=xChange, yPlayerLocation+=yChange"
for %%X in (!xPlayerLocation!) do for %%Y in (!yPlayerLocation!) do (
if not !px%%X%y%%Y!==%blank% REM doSomething
)
...
)
or
setlocal enableDelayedExpansion
...
REM This also works
for ... in (...) do (
...
set /a "xPlayerLocation+=xChange, yPlayerLocation+=yChange"
for /f "tokens=1,2" %%X in ("!xPlayerLocation! !yPlayerLocation!") do (
if not !px%%X%y%%Y!==%blank% REM doSomething
)
...
)
If you are serious about developing a high quality game using batch, then you may be interested in studying the techniques I used in developing SNAKE.BAT - an arcade style game using only native batch commands. The beginning of the post is a bunch of code, but afterwards I describe a number of techniques I used for improving performance. It is advanced stuff, so don't try to absorb everything at once. Absorb what you can, and then revisit the post later on after you gain more experience.