Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Batch file save/load variables  (Read 28700 times)

0 Members and 1 Guest are viewing this topic.

Risen91

    Topic Starter


    Rookie

    • Experience: Beginner
    • OS: Unknown
    Batch file save/load variables
    « 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.

    Geek-9pm


      Mastermind
    • Geek After Dark
    • Thanked: 1026
      • Gekk9pm bnlog
    • Certifications: List
    • Computer: Specs
    • Experience: Expert
    • OS: Windows 10
    Re: Batch file save/load variables
    « 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.

    Raven19528



      Hopeful
    • Thanked: 30
      • Computer: Specs
      • Experience: Experienced
      • OS: Windows 7
      Re: Batch file save/load variables
      « 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.
      "All things that are
      Are with more spirit chased than enjoy'd" -Shakespeare

      Risen91

        Topic Starter


        Rookie

        • Experience: Beginner
        • OS: Unknown
        Re: Batch file save/load variables
        « 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

        Geek-9pm


          Mastermind
        • Geek After Dark
        • Thanked: 1026
          • Gekk9pm bnlog
        • Certifications: List
        • Computer: Specs
        • Experience: Expert
        • OS: Windows 10
        Re: Batch file save/load variables
        « 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.

        Squashman



          Specialist
        • Thanked: 134
        • Experience: Experienced
        • OS: Other
        Re: Batch file save/load variables
        « 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

        Risen91

          Topic Starter


          Rookie

          • Experience: Beginner
          • OS: Unknown
          Re: Batch file save/load variables
          « 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.

          Salmon Trout

          • Guest
          Re: Batch file save/load variables
          « 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 »

          Salmon Trout

          • Guest
          Re: Batch file save/load variables
          « 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 »

          Salmon Trout

          • Guest
          Re: Batch file save/load variables
          « 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

          Risen91

            Topic Starter


            Rookie

            • Experience: Beginner
            • OS: Unknown
            Re: Batch file save/load variables
            « Reply #10 on: January 22, 2012, 07:45:19 AM »
            Thanks loads for help I should be able to work with that :)

            Squashman



              Specialist
            • Thanked: 134
            • Experience: Experienced
            • OS: Other
            Re: Batch file save/load variables
            « 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


              Rookie

              • Experience: Beginner
              • OS: Unknown
              Re: Batch file save/load variables
              « 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



                Specialist
              • Thanked: 134
              • Experience: Experienced
              • OS: Other
              Re: Batch file save/load variables
              « 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


              • Genius
              • Maud' Dib
              • Thanked: 1769
                • Yes
              • Experience: Beginner
              • OS: Windows 7
              Re: Batch file save/load variables
              « Reply #14 on: January 22, 2012, 08:22:51 AM »
              No need to holler...
              " Anyone who goes to a psychiatrist should have his head examined. "