1.

Solve : Batch file save/load variables?

Answer»

Hey, im having some PROBLEMS finding how to overwrite multiple variables to 1 .bat file with mutiple lines. Basicly im making a small Rpg game and there is a few stats (e.g. strength, health, intellect) that i want to write to one file such as:
set strength=25
set health= 100
set intellect 50
within the main batch file im using the command echo set health=%health%>health.bat, but ideally i want to put all these variables into 1 file. This is where the problem comes in, any ideas how I would go about overwriting say the strength without affecting the others? I thought of using something like:
 FOR /F "tokens=2 delims=," %%G IN (stats.bat) DO echo %%G %%H
and having them laid out with in 1 line with commas separating each variable, but I still cant figure out how to use a command to overwrite the specific one needed and also set that variable into the main file (as I don't want to just echo it).

If there is any more information I have missed or if you need anything clarified just ask,
Thanks in advance.You have more tools available to you by using the WSH instead of just plain batch. Batch is so 1990 ish. It is so retro.
In the search box above enter WSH and find articles that have talked about this before.Well, you would probably have an easier time by reading and writing the stats at the same time. So this: Code: [Select]for /f "delims=," %%A in (stats.bat) do (
  set health=%%A
  set strength=%%B
  ...
)could be used to read your stats and a simple:
Code: [Select]echo %health%,%strength%,...>stats.batto write the stats back. It's easier to do it this way in batch, but as Geek suggested, you may have an easier time doing this in another language.Hey guys, thanks for the REPLY but I'm still having a little trouble with it, first off just wondering in your 2 codes Raven would the first 1 read MULTI line but the second one only save to single line? and when I try the code I get this:

and to reply to Geek-9pm i have never even heard of WSH and most of the game is already created im just working on loading/saving the game now, thanks for the tip though ill be looking it up Quote

and to reply to Geek-9pm i have never even heard of WSH and most of the game is already created im just working on loading/saving the game now, thanks for the tip though ill be looking it up
Sorry I did not explain. Microsoft encourages users to move away from using just batch for non-trivial tanks.
Quote
Windows Script Host
From Wikipedia, the free encyclopedia
  (Redirected from WSH)
http://en.wikipedia.org/wiki/WSH

The Microsoft Windows Script Host (WSH) is an automation technology for Microsoft Windows operating systems that provides scripting capabilities comparable to batch files, but with a greater range of supported features. It was originally called Windows Scripting Host, but was renamed for the second release.
WSH lets you use neither the command line or a windows interface.  It allows you to use other scripts as part of a task.  Even two different languages in the same task. I wish CH would have a WSH area in addition to the DOS area. Many new users don't know that more advanced tools are built-in to current version of Windows. End of Ran t. Pardon me.
settings.ini
Code: [Select]strength=25
health=100
intellect=50ini.bat
Code: [Select]echo off

REM Reading in Property file
for /f "tokens=1,2 delims==" %%G in (settings.ini) do set %%G=%%H

REM echoing the values
echo Strength %strength%
echo Health %health%
echo Intellect %intellect%

REM Changing the values of each
set /a strength+=10
set /a health-=5
set /a intellect-=8

REM echoing the changed values
echo Strength %strength%
echo Health %health%
echo Intellect %intellect%

REM Writing the changes back to the settings file
for %%I in (strength health intellect) do (
setlocal enabledelayedexpansion
TYPE settings.ini | find /v "%%I=">settings.tmp
move /y settings.tmp settings.ini >nul
echo %%I=!%%I!>>settings.ini
)
type settings.iniOutput
Code: [Select]Strength 25
Health 100
Intellect 50
Strength 35
Health 95
Intellect 42
strength=35
health=95
intellect=42Thanks for the reply squash, but I need the stats to be overwritten rather than flooding the file with updated ones. Any idea how I could change your code to achieve this?
Thanks.Settings.ini

Code: [Select]strength=25
health=100
intellect=50

If the loop variable (the line read from file by FOR /F) is of the format variablename=value then you can just put the set keyword in front of it:

Code: [Select]echo off

REM Read settings from file
for /f %%S in (settings.ini) do set %%S

REM Show values stored in memory
echo strength  %strength%
echo health    %health%
echo intellect %intellect%

REM Alter a setting
set /p health="Enter new value for health ? "

REM Write settings to file
echo strength=%strength%    > settings.ini
echo health=%health%       >> settings.ini
echo intellect=%intellect% >> settings.ini

or more compact... choose a unique variable name prefix and then you can use Set to do all the work of writing the values to file with one command.

settings.ini:

Code: [Select]MyVarStrength=100
MyVarHealth=50
MyVarIntellect=65

Code: [Select]echo off

REM Read settings from file
for /f %%S in (settings.ini) do set %%S

REM Show settings now stored in memory
echo strength  %MyVarStrength%
echo health    %MyVarHealth%
echo intellect %MyVarIntellect%

REM alter a setting in memory
set /p MyVarHealth="Enter new value for health ? "

REM Write settings to file
set MyVar > settings.ini


One difference is that after a write to file the variable names in settings.ini will be in alphabetical order of name. Like this...

Code: [Select]MyVarHealth=67
MyVarIntellect=65
MyVarStrength=100


another idea... run counter and last run logger

Initial state of Runcount.log

Code: [Select]MyProgramVarRuncounter=1
MyProgramVarLastRun=First run
Code: [Select]echo off

REM Read values from file
for /f "tokens=*" %%S in (Runcount.log) do set %%S
echo Script has been run %MyProgramVarRuncounter% time(s) Last run date/time  %MyProgramVarLastRun%

REM increment counter
set /a MyProgramVarRuncounter+=1

REM Get date and time into variable
set MyProgramVarLastRun=%date% %time%

REM Write new values to file
set MyProgramVar > Runcount.log
Code: [Select]C:\>Lastrundemo.bat
Script has been run 1 time(s) Last run date/time  First run
C:\>Lastrundemo.bat
Script has been run 2 time(s) Last run date/time  22/01/2012 14:20:15.86
C:\>Lastrundemo.bat
Script has been run 3 time(s) Last run date/time  22/01/2012 14:20:20.61
C:\>Lastrundemo.bat
Script has been run 4 time(s) Last run date/time  22/01/2012 14:20:27.77
C:\>Lastrundemo.bat
Script has been run 5 time(s) Last run date/time  22/01/2012 14:20:30.14
C:\>Lastrundemo.bat
Script has been run 6 time(s) Last run date/time  22/01/2012 14:20:41.33
C:\>Lastrundemo.bat
Script has been run 7 time(s) Last run date/time  22/01/2012 14:22:14.76
Thanks loads for help I should be able to work with that Quote from: Risen91 on January 22, 2012, 05:04:33 AM
Thanks for the reply squash, but I need the stats to be overwritten rather than flooding the file with updated ones. Any idea how I could change your code to achieve this?
Thanks.

You obviously didn't run my code.  If you look at the output you will see after changing the values and rewriting the values to the stats file that the only values back in the file are the original value names with updated stats numbers.  I don't know what you are thinking it is doing. Quote from: Squashman on January 22, 2012, 07:51:46 AM
You obviously didn't run my code.  If you look at the output you will see after changing the values and rewriting the values to the stats file that the only values back in the file are the original value names with updated stats numbers.  I don't know what you are thinking it is doing.

Ahhh, I thought by "output" you meant what would be in the file after the code had run, my apologies and thank you again, Ill have a look into all suggestions and see what code fits best then. Quote from: Risen91 on January 22, 2012, 07:57:29 AM
Ahhh, I thought by "output" you meant what would be in the file after the code had run, my apologies and thank you again, Ill have a look into all suggestions and see what code fits best then.

The OUTPUT of the batch file!!!! You obviously didn't even bother to read the code and SEE THE SIX ECHO STATEMENTS IN THERE AND THE TYPE COMMAND THAT WAS OUTPUTTING WHAT IT NOW IN THE STATS FILE!No need to holler...


Discussion

No Comment Found