Risen91 Topic Starter
Posts: 24
Experience: Beginner OS: Unknown
|
 |
« on: January 20, 2012, 05:42:21 AM » |
|
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.
|
|
|
|
|
|
|
Raven19528
Thanked: 29 Posts: 284
Computer: Specs Experience: Experienced OS: Windows 7

|
 |
« Reply #2 on: January 20, 2012, 02:32:14 PM » |
|
Well, you would probably have an easier time by reading and writing the stats at the same time. So this: for /f "delims=," %%A in (stats.bat) do ( set health=%%A set strength=%%B ... )could be used to read your stats and a simple:
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.
|
"All things that are Are with more spirit chased than enjoy'd" -Shakespeare
|
|
|
Risen91 Topic Starter
Posts: 24
Experience: Beginner OS: Unknown
|
 |
« Reply #3 on: January 20, 2012, 09:17:29 PM » |
|
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 
|
|
|
|
|
|
|
Squashman
Thanked: 25 Posts: 341
Experience: Experienced OS: Other

|
 |
« Reply #5 on: January 21, 2012, 02:19:55 PM » |
|
settings.ini
strength=25 health=100 intellect=50ini.bat
@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
Strength 25 Health 100 Intellect 50 Strength 35 Health 95 Intellect 42 strength=35 health=95 intellect=42
|
|
|
|
|
Risen91 Topic Starter
Posts: 24
Experience: Beginner OS: Unknown
|
 |
« Reply #6 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.
|
|
|
|
|
|
|
|
|
|
|
Risen91 Topic Starter
Posts: 24
Experience: Beginner OS: Unknown
|
 |
« Reply #10 on: January 22, 2012, 07:45:19 AM » |
|
Thanks loads for help I should be able to work with that 
|
|
|
|
|
Squashman
Thanked: 25 Posts: 341
Experience: Experienced OS: Other

|
 |
« Reply #11 on: January 22, 2012, 07:51:46 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.
|
|
|
|
|
Risen91 Topic Starter
Posts: 24
Experience: Beginner OS: Unknown
|
 |
« Reply #12 on: January 22, 2012, 07:57:29 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.
|
|
|
|
|
Squashman
Thanked: 25 Posts: 341
Experience: Experienced OS: Other

|
 |
« Reply #13 on: January 22, 2012, 08:18:04 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!
|
|
|
|
|
patio Moderator
Thanked: 1069 Posts: 11,351
Experience: Beginner OS: Windows 7

Maud' Dib
|
 |
« Reply #14 on: January 22, 2012, 08:22:51 AM » |
|
No need to holler...
|
" All generalizations are false, including this one. "
|
|
|
Risen91 Topic Starter
Posts: 24
Experience: Beginner OS: Unknown
|
 |
« Reply #15 on: January 22, 2012, 10:27:01 AM » |
|
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!
dude chill out? When you put the output as: Strength 25 Health 100 Intellect 50 Strength 35 Health 95 Intellect 42 strength=35 health=95 intellect=42 I thought that it was simply adding the new values to the end, not overwriting them... and no I didn't bother to read it, want to know why? Because I don't understand the code, I'm new to batch files. Simple misunderstanding that's all.
|
|
|
|
|
Squashman
Thanked: 25 Posts: 341
Experience: Experienced OS: Other

|
 |
« Reply #16 on: January 22, 2012, 03:04:44 PM » |
|
dude chill out? When you put the output as:I thought that it was simply adding the new values to the end, not overwriting them... and no I didn't bother to read it, want to know why? Because I don't understand the code, I'm new to batch files. Simple misunderstanding that's all.
So are you going to just run any code someone gives you just willy nilly without knowing what the code does? For all you know I could have thrown in some code to delete all your My documents! I could have sat there and called a bunch of sub routines or built some other undecipherable variable that when expanded would would delete all your My Documents. If you don't understand the code you should have asked what the code was doing or at least ran it to see what it was doing instead of assuming it did something. Because we all know what happens when we assume!
|
|
|
|
|
Risen91 Topic Starter
Posts: 24
Experience: Beginner OS: Unknown
|
 |
« Reply #17 on: January 22, 2012, 03:25:52 PM » |
|
"or at least ran it to see what it was doing instead of assuming it " you just contradicted yourself by telling me not to run code that I don't understand, then telling me to run it despite not knowing  Its a help forum, I asked for help and I got the help I needed, I know a little and that the code you showed me isn't malicious, but not enough to know the things like: echo %%I=!%%I!>>settings.ini Thread closed I got the help I needed.
|
|
|
|
|
|
|
|
|
Squashman
Thanked: 25 Posts: 341
Experience: Experienced OS: Other

|
 |
« Reply #20 on: January 22, 2012, 03:39:38 PM » |
|
What happens when we rant immoderately?
Apparently I become that same guy. I guess my whole point was that I find it very disrespectful when people don't even bother to understand the code and then just say that it won't work for them. We all spend hours upon hours a week helping people on the Internet and the least they could do is take the time to learn what we are trying to teach. You know the old saying "Give a man a fish.....Teach a man too fish...." In this instance I think it was the former.
|
|
|
|
|
|
|
patio Moderator
Thanked: 1069 Posts: 11,351
Experience: Beginner OS: Windows 7

Maud' Dib
|
 |
« Reply #22 on: January 22, 2012, 03:53:38 PM » |
|
Or maybe take a break for awhile...
|
" All generalizations are false, including this one. "
|
|
|
Squashman
Thanked: 25 Posts: 341
Experience: Experienced OS: Other

|
 |
« Reply #23 on: January 22, 2012, 05:48:15 PM » |
|
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:
MyVarStrength=100 MyVarHealth=50 MyVarIntellect=65
@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...
MyVarHealth=67 MyVarIntellect=65 MyVarStrength=100
I like this a lot better than my code.
|
|
|
|
|