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





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)

(see pic)

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

@echo off



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


color F0

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

goto start

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?"

goto saveconfirm



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%


REM exports the age to a file


echo %age%


exports the gender to a file


echo %gender%


echo Successfully saved as %savefile%.sgf


goto start

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


color FC

echo something went wrong (file does not exist)


goto start



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


goto start

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

the file

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



color FC



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



REM a little confirmination screen

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


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



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


goto start



color FC

echo you did not delete %delete%.sgf


goto start

Step 6: Download (if You Want)

you can download a txt version of the file here:

thank you for reading this instructable hope you enjoyed!



    • Epilog Challenge 9

      Epilog Challenge 9
    • Pocket-Sized Contest

      Pocket-Sized Contest
    • Science of Cooking

      Science of Cooking

    We have a be nice policy.
    Please be positive and constructive.




    if you know how the for command works there is an easier way you can find the file here:

    for those who know how for works here's the same code but made easier (you can't delete any of the files with this one)

    @echo off

    REM the directory i'll be using

    cd var



    color F0

    echo a - load

    echo z - save

    set /p input=""

    if %input%==a goto load

    if %input%==A goto load

    if %input%==z goto save

    if %input%==Z goto save

    goto start



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


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


    set /p gender="Male or Female?: "


    set /p music="Favorite music style?: "


    set /p save="Save as?: "

    goto savecon



    echo %name%_%age%_%gender%_%music%


    goto start



    echo Load


    set /p load="Load which file?: "

    if exist %load%.sgf goto loadtrue

    if not exist %load%.sgf goto loadfail



    color FC

    echo error, file does not exist


    goto start



    REM we need 4 tokens (4 values) delims is what seperates these with a _

    REM example My_Dog_is_cute contains 4 tokens when the delimiter is not

    REM defined it will be a space but i used _ as delimiter

    REM My = token 1 Dog = token 2 is = token 3 cute = token 4

    for /f "tokens=1,2,3,4 delims=_" %%a in (%load%.sgf) do (

    echo Name: %%a

    echo Age: %%b

    echo Gender: %%c

    echo Music style: %%d



    goto start