Question:
how do i save variables in batch?
anonymous
2010-10-05 21:11:46 UTC
whenever i close my batch file i lose my variables. i want to be able to save my Variable and load them later. i know that you can log when something happens in a batch file, so i was wondering if cmd could open that file and retrieve data from it.

thanks for the help
Four answers:
anonymous
2010-10-09 20:30:57 UTC
Output batch commands to a batch file which will set your variables. Later you can use the call command to invoke that batch file and get your variables back. Example 1:



@echo off

echo set myvar=my first value>vars.bat

echo set myvar2=my second value>>vars.bat

echo set myvar3=my third value>>vars.bat

call vars.bat

echo %myvar%

echo %myvar2%

echo %myvar3%

pause

exit



Example 2:



@echo off

if not exist vars.bat (

type nul>vars.bat

)

:menu

cls

echo 1) Make new variable

echo 2) View a variable

echo.

set /p cin=Select option:

if %cin%==1 goto mkvar

if %cin%==2 goto getvar

goto menu

:mkvar

cls

set /p var=Enter variable name:

set /p val=Enter value:

echo set %var%=%val%>>vars.bat

goto menu

:getvar

cls

if exist vars.bat call vars.bat

cls

set /p var=Enter variable name:

setlocal enabledelayedexpansion

echo Value of %var% is !%var%!

endlocal

pause

goto menu
anonymous
2016-10-19 16:50:16 UTC
Variables In Batch
herzog
2016-10-14 09:02:07 UTC
EDIT: Uh, superb Frederic, yet did you attempt working the batch checklist? you will get this form of output: echo (man or woman inputted call) a million>username.txt The text textile fabric gets outputted, yet you have the greater desirable suited rubbish displayed from that line. You forgot the @ in front of the echo. :) in case you typed something like that from the command-line, it relatively works cleanly inspite of the reality that. echo %course% > course.txt (I initially tried it with out the 'echo', so by way of fact it somewhat is why i ought to no longer get it to artwork.) EDIT 2: Oops, I pressed the incorrect button - I meant to grant Frederic a thumbs up. Sorry. -- no longer via a batch checklist. you will could desire to jot down down a application to try this. as quickly as you're making use of domicile domicile windows, then you definately can study to coach domicile domicile windows' equipped-in seen man or woman-friendly scripting somewhat of batch documents by way of actuality VB script facilitates you to do many things and you're certainly no longer limited to a console window.
Vasili
2010-10-06 05:37:58 UTC
try this



echo bla bla > file.txt

set /p myvar= < file.txt

echo %myvar%


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...