Let us make the job of saving values of your batch game as simple as possible by using environment variables for your game variables which all start with same prefix string like MyGame_
:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "GameDataFile=%APPDATA%\MyGame\MyGame.dat"
if exist "%GameDataFile%" (
for /F "usebackq tokens=1,2* delims=_=" %%A in ("%GameDataFile%") do if "%%A" == "MyGame" set "%%A_%%B=%%C"
goto OutputValues
)
:NameInput
set "MyGame_Name="
set /P "MyGame_Name=Please enter your name: "
if not defined MyGame_Name goto NameInput
set "MyGame_Experience=0"
set "MyGame_Money=3"
set "MyGame_Deaths=0"
set "MyGame_Level=1"
set "MyGame_Health=5"
set "MyGame_Power=10"
:OutputValues
cls
echo Name: %MyGame_Name%
echo Experience Points: %MyGame_Experience%
echo Gold: %MyGame_Money%
echo Total Deaths: %MyGame_Deaths%
echo Current Level: %MyGame_Level%
echo Total Health Points: %MyGame_Health%
echo Power Level: %MyGame_Power%
echo/
pause
rem Other command lines for playing the game ...
:SaveValues
for /F "delims=" %%I in ("%GameDataFile%") do md "%%~dpI" 2>nul
set MyGame_ >"%GameDataFile%"
endlocal
Microsoft recommends saving application data into a company or application related subdirectory of user specific application data directory of which path is defined by environment variable APPDATA
. That is the reason for using %APPDATA%\%~n0\%~n0.dat
whereby %APPDATA%
is expanded by Windows command interpreter by the path of this environment variable and %~n0
by name of batch file without path and file extension.
In case of this file exists already each non empty line of the file not starting with a semicolon is read by FOR and split up into three substrings (tokens):
First substring is from beginning of line to underscore which should be always the string MyGame
if the file was not modified by somebody using a text editor. This string is assigned to loop variable A
.
Second substring is the name of the environment variable between underscore and equal sign which separates variable name from variable value. This substring is assigned to next loop variable according to ASCII table which is B
.
And third substring is everything after equal sign to end of line which is the variable value assigned to loop variable C
.
The IF condition makes sure to load from the file for security just the environment variables starting case-sensitive with MyGame_
. This IF condition would not be really needed, but it makes the batch file a little bit safer against data manipulations.
In general it would be good to check each value read from file for valid data after having them assigned to the environment variables. This data check is skipped in the demo batch code and instead the read values are output next.
But if the file does not already exist, the batch file user is asked for the name and the other environment variables are defined with default values.
Saving the values of the environment variables of the game is very easy now with all related environment variables starting with MyGame_
. First the subdirectory is created in application data directory of the user with suppressing the error message output in case of directory existing already.
Then command SET is used to print all environment variables starting with MyGame_
sorted alphabetically with redirecting this output into the file.
This method makes it very easy to add later additional environment variables starting with MyGame_
as just the initialization must be done if not existing after loading the data from file. Loading and saving the additional variable starting with MyGame_
is automatically done by existing code. Also it does not matter anymore in which order the values are stored in the data file.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
call /?
... explains %~n0
(name of batch file).
cls /?
echo /?
endlocal /?
for /?
goto /?
if /?
md /?
pause /?
rem /?
set /?
setlocal /?
See also: