Introduction: Saving & Loading Variables in Batch (external File(s))
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.
Step 1: Create the Folder for the Program
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)
Step 2: The Main Screen of Your Program
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
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...
echo a - save
echo z - load
echo e - delete
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
Step 3: Saving the Variables
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)
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?"
set /p age ="How old are you?"
set /p gender="Male or Female?"
REM we will ask the user to save the file as.., this name is needed for loading later
set /p savefile="Save file as?"
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...
REM exports the age to a file
exports the gender to a file
echo Successfully saved as %savefile%.sgf
Step 4: Loading the Variables
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)
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
echo something went wrong (file does not exist)
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%
REM it clarifies which file is loaded
echo file loaded %load%.sgf
Step 5: Deleting Files
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
(NOTE: we are starting from the :delete code)
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?:"
REM a little confirmination screen
echo are you sure you want to delete %delete%.sgf?
set /p yn=""
if %yn%==y goto dely
if %yn%==Y goto dely
if %yn%==n goto deln
if %yn%==N goto deln
REM deletes the file with the name value of %delete% 1, 2, 3 .file extension used
echo successfully deleted %delete%.sgf
echo you did not delete %delete%.sgf
Step 6: Download (if You Want)
you can download a txt version of the file here: https://goo.gl/0aeyNA
thank you for reading this instructable hope you enjoyed!