| 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: 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 upSorry I did not explain. Microsoft encourages users to move away from using just batch for non-trivial tanks. Quote Windows Script HostWSH 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? 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... |
|