Saving & Loading Variables in Batch (external File(s))

by Nasamos in Circuits > Computers

10898 Views, 8 Favorites, 0 Comments

Saving & Loading Variables in Batch (external File(s))

Screenshot (15).png

here's another small program which let's you save variables in an external document and load it later

even after closing the cmd!, follow along if you want or go to the last step to find a download link.

Create the Folder for the Program

Screenshot (16).png

First you'll have to create a new folder and inside the folder you'll have to place another folder and the .bat file

you can easily do this by tapping ctrl+shift+N on the desktop

then inside your newly made folder place another one (it's not a necessity but it looks cleaner & easier to manage)

inside the folder you made on your Desktop will be 2 files (the .bat file and the folder)

(see pic)

The Main Screen of Your Program

Screenshot (17).png

Here we'll see the first part of the code, creating the main screen and it's functions will be done here

i use the functions on ("a, z, e") because i have a azerty keyboard so feel free to change it

@echo off

cls

:start

REM this will 'cd' change directory to current path + 'Variables'

REM you can use any map name but make sure you change it here too then...

cd Variables

cls

color F0

echo a - save

echo z - load

echo e - delete

echo.

set /p choice=""

if %choice%==a goto save

if %choice%==A goto save

if %choice%==z goto load

if %choice%==Z goto load

if %choice%==e goto delete

if %choice%==E goto delete

goto start

Saving the Variables

Screenshot (19).png

Here we'll save the "variables" in text files (.sgf) is the extension i use (savegamefile) you can use any extension like .dll, .txt, etc..

(NOTE: We are starting from the :save code)

:save

cls

REM here we'll ask questions which will be stored in an external file (the values)

REM we will store 3 values, the name, age and the gender of the user

set /p name="What's your full name?"

cls

set /p age ="How old are you?"

cls

set /p gender="Male or Female?"

cls

REM we will ask the user to save the file as.., this name is needed for loading later

set /p savefile="Save file as?"

goto saveconfirm

:saveconfirm

cls

color F2

REM this exports the name to a file with the same name as the "%savefile%"1.sgf

REM the 1 is because we have 3 values we will store them all in seperate files

REM this is easier to load, the .sgf is just the extension i use (savegamefile)

REM you can use .dll, .txt, etc...

(

echo %name%

)>%savefile%1.sgf

REM exports the age to a file

(

echo %age%

)>%savefile%2.sgf

exports the gender to a file

(

echo %gender%

)>%savefile%3.sgf

echo Successfully saved as %savefile%.sgf

pause

goto start

Loading the Variables

Screenshot (20).png

Here we'll create the code for loading the variables (you need to save some first before it works)

(NOTE: we are starting from the :load code)

:load

cls

REM asks to load which file

set /p load="Load which file?"

REM if the variable of the name (number 1 file) does not exist we'll conclude that none of the variables exist thus REM error follows

if not exist %load%1.sgf goto error1

if exist %load%1.sgf goto proceed

:error1

color FC

echo something went wrong (file does not exist)

pause

goto start

:proceed

cls

REM loads the variable of the name (in the text file (1)) and puts it in a variable in the current session (namesave)

REM if you noticed the numbers this is again because we saved the file(s) like this 1 = name 2 = age 3 = gender

set /p namesave=<%load%1.sgf

REM does the same but for age

set /p agesave=<%load%2.sgf

REM same for the gender

set /p gendersave=<%load%3.sgf

echo Name: %namesave%

echo Age: %agesave%

echo Gender: %gendersave%

echo.

REM it clarifies which file is loaded

echo file loaded %load%.sgf

pause

goto start

Deleting Files

Screenshot (21).png

Well we can save save and load files already but what if you have quite a lot of them

delete them of course this command will delete the file you want to be gone it will handle (1, 2 and 3) of

the file

(NOTE: we are starting from the :delete code)

:delete

cls

color FC

echo DELETE

echo.

REM note the difference between loading and deleting

REM when loading it will check if the file exists when deleting it won't

set /p delete="Delete which file?:"

goto delnext

:delnext

cls

REM a little confirmination screen

echo are you sure you want to delete %delete%.sgf?

echo.

echo Y/N

set /p yn=""

if %yn%==y goto dely

if %yn%==Y goto dely

if %yn%==n goto deln

if %yn%==N goto deln

goto delnext

:dely

cls

REM deletes the file with the name value of %delete% 1, 2, 3 .file extension used

del %delete%1.sgf

del %delete%2.sgf

del %delete%3.sgf

color F2

echo successfully deleted %delete%.sgf

pause

goto start

:deln

cls

color FC

echo you did not delete %delete%.sgf

pause

goto start

Download (if You Want)

Screenshot (24).png
Screenshot (22).png
Screenshot (23).png

you can download a txt version of the file here: https://goo.gl/0aeyNA

thank you for reading this instructable hope you enjoyed!