loading

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

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

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)

: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

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)

: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

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)

: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

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!

<p>if you know how the for command works there is an easier way you can find the file here: https://goo.gl/2uGowk</p>
<p>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)</p><p>@echo off</p><p>REM the directory i'll be using</p><p>cd var</p><p>:start</p><p>cls</p><p>color F0</p><p>echo a - load</p><p>echo z - save</p><p>set /p input=&quot;&quot;</p><p>if %input%==a goto load</p><p>if %input%==A goto load</p><p>if %input%==z goto save</p><p>if %input%==Z goto save</p><p>goto start</p><p>:save</p><p>cls</p><p>set /p name=&quot;What's your name?: &quot;</p><p>cls</p><p>set /p age=&quot;How old are you?: &quot;</p><p>cls</p><p>set /p gender=&quot;Male or Female?: &quot;</p><p>cls</p><p>set /p music=&quot;Favorite music style?: &quot;</p><p>cls</p><p>set /p save=&quot;Save as?: &quot;</p><p>goto savecon</p><p>:savecon</p><p>(</p><p>echo %name%_%age%_%gender%_%music%</p><p>)&gt;%save%.sgf</p><p>goto start</p><p>:load</p><p>cls</p><p>echo Load</p><p>echo.</p><p>set /p load=&quot;Load which file?: &quot;</p><p>if exist %load%.sgf goto loadtrue</p><p>if not exist %load%.sgf goto loadfail</p><p>:loadfail</p><p>cls</p><p>color FC</p><p>echo error, file does not exist</p><p>pause</p><p>goto start</p><p>:loadtrue</p><p>cls</p><p>REM we need 4 tokens (4 values) delims is what seperates these with a _</p><p>REM example My_Dog_is_cute contains 4 tokens when the delimiter is not</p><p>REM defined it will be a space but i used _ as delimiter</p><p>REM My = token 1 Dog = token 2 is = token 3 cute = token 4</p><p>for /f &quot;tokens=1,2,3,4 delims=_&quot; %%a in (%load%.sgf) do (</p><p>echo Name: %%a</p><p>echo Age: %%b</p><p>echo Gender: %%c</p><p>echo Music style: %%d</p><p>)</p><p>pause</p><p>goto start</p>

About This Instructable

392views

8favorites

Bio: i'm studying car mechanics @sint-jozefinstituut Geel, English is not my native language...
More by Nasamos:Batch login screen Saving & Loading variables in batch (external file(s)) 
Add instructable to: