Home / Microsoft / Microsoft DOS / Batch file save/load variables
0 Members and 2 Guests are viewing this topic. « previous next »
Pages: 1 2 [All] - (Bottom) Print
Author Topic: Batch file save/load variables  (Read 580 times)
Risen91
Topic Starter
Rookie



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.
IP logged
Geek-9pm
Sage



Thanked: 373
Posts: 8,928

Computer: Specs
Experience: Expert
OS: Windows XP


Geek After Dark

Geek 9pm blog
« Reply #1 on: January 20, 2012, 01:49:39 PM »

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.
IP logged

Raven19528
Hopeful



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:
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.
IP logged

"All things that are
Are with more spirit chased than enjoy'd" -Shakespeare
Risen91
Topic Starter
Rookie



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 :P
IP logged
Geek-9pm
Sage



Thanked: 373
Posts: 8,928

Computer: Specs
Experience: Expert
OS: Windows XP


Geek After Dark

Geek 9pm blog
« Reply #4 on: January 21, 2012, 12:31:00 AM »

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 :P
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.
IP logged

Squashman
Hopeful



Thanked: 25
Posts: 341

Experience: Experienced
OS: Other



« Reply #5 on: January 21, 2012, 02:19:55 PM »

settings.ini
Code: [Select]
strength=25
health=100
intellect=50
ini.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.ini
Output
Code: [Select]
Strength 25
Health 100
Intellect 50
Strength 35
Health 95
Intellect 42
strength=35
health=95
intellect=42
IP logged
Risen91
Topic Starter
Rookie



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.
IP logged
Salmon Trout
Sage



Thanked: 546
Posts: 7,950

Computer: Specs
Experience: Beginner
OS: Unknown

1
« Reply #7 on: January 22, 2012, 05:57:09 AM »

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

« Last Edit: January 22, 2012, 06:20:07 AM by Salmon Trout » IP logged


Proud to be European
Salmon Trout
Sage



Thanked: 546
Posts: 7,950

Computer: Specs
Experience: Beginner
OS: Unknown

1
« Reply #8 on: January 22, 2012, 06:15:05 AM »

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


« Last Edit: January 22, 2012, 06:42:50 AM by Salmon Trout » IP logged


Proud to be European
Salmon Trout
Sage



Thanked: 546
Posts: 7,950

Computer: Specs
Experience: Beginner
OS: Unknown

1
« Reply #9 on: January 22, 2012, 07:28:29 AM »

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
IP logged


Proud to be European
Risen91
Topic Starter
Rookie



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 :)
IP logged
Squashman
Hopeful



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.
IP logged
Risen91
Topic Starter
Rookie



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.
IP logged
Squashman
Hopeful



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!
IP logged
patio
Moderator
Genius



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...
IP logged

   
"
All generalizations are false, including this one.  "
Risen91
Topic Starter
Rookie



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:
Quote
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.
IP logged
Squashman
Hopeful



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!

IP logged
Risen91
Topic Starter
Rookie



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.
IP logged
Salmon Trout
Sage



Thanked: 546
Posts: 7,950

Computer: Specs
Experience: Beginner
OS: Unknown

1
« Reply #18 on: January 22, 2012, 03:33:11 PM »

Because we all know what happens when we assume!

What happens when we rant immoderately?

IP logged


Proud to be European
Salmon Trout
Sage



Thanked: 546
Posts: 7,950

Computer: Specs
Experience: Beginner
OS: Unknown

1
« Reply #19 on: January 22, 2012, 03:38:27 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

Well, I thought that at first, and I been dealing with batch files since Moses was knee high to a mezuzah. Risen91, please don't assume (!) we're all as crabby as that other guy.

IP logged


Proud to be European
Squashman
Hopeful



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.
IP logged
Salmon Trout
Sage



Thanked: 546
Posts: 7,950

Computer: Specs
Experience: Beginner
OS: Unknown

1
« Reply #21 on: January 22, 2012, 03:44:54 PM »

I find it very disrespectful when people don't even bother to understand the code

You're going to see a lot of that at Computerhope. For the sake of your own sanity learn to get over it.

IP logged


Proud to be European
patio
Moderator
Genius



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...
IP logged

   
"
All generalizations are false, including this one.  "
Squashman
Hopeful



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:

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
I like this a lot better than my code.
IP logged
Pages: 1 2 [All] - (Top) Print 
Home / Microsoft / Microsoft DOS / Batch file save/load variables « previous next »
 


Login with username, password and session length

Old Forum Search | Forum Rules
Copyright © 2010 Computer Hope ® All rights reserved.
Powered by SMF 2.0 RC3 | SMF © 2006–2010, Simple Machines LLC
Page created in 0.185 seconds with 19 queries.