0

I'm making a program on batch with a lot of data values. I need to load them in chunks, but I'll probably end up saving them all together. How would I load them from a specific group if I loaded values to the .sav file using this format?

(
    echo "[playerdata]"
    echo playername=%playername%
    echo playerlevel=%playerlevel%
    echo "[inventory]"
    echo playerslotone=%playerslotone%
    echo plrslot1amnt=%plrslot1amnt%
) > savegame.sav

I might need to remove the quotations from the [player data] and [inventory] headers. This is just an example there will be more values and more segments. I need to load an exact value from a segment, like you would from an ini.(But not from an InI) Technoguyfication used this format in his ini file in his response to [Save and Load .bat game] Any help is appreciated. I'm a severe beginner so please consider this.

I'm sorry if this seems to be a duplicate. It shouldn't be, as I'm loading and saving from a .sav file, and using a new path as opposed to just a command. Compo's response was extremely helpful in this situation. I could be wrong, as I said I'm a severe beginner.

My apologies, and thanks.

Faraday92
  • 3
  • 2
  • I will consider this if you clarify your question a bit, by giving examples of your grouping requirements, what do they look like and what should the result be? – Gerhard Apr 13 '18 at 10:08
  • I don't understand what you want, but I wonder if the solutions given at [this question](https://stackoverflow.com/questions/2866117/windows-batch-script-to-read-an-ini-file) (that I taken from your linked answer) may work in this case. If so, ... – Aacini Apr 13 '18 at 10:22
  • looks like each key (variable name) is unique and sections are only for human readability and can be ignored. Please confirm or deny (would make a solution quite simple) – Stephan Apr 13 '18 at 15:01
  • Compo's response can better explain this than I can, he seems to have understood what I was asking. Probably, even more so than me. My apologies. – Faraday92 Apr 14 '18 at 06:39

1 Answers1

0

It would certainly be adviseable to remove those doublequotes enclosing the section names.

You could even make your .sav file look prettier, by using TABs and  s, if you wanted to, e.g.

(
    Echo [PlayerData]
    Echo PlayerName     = %PlayerName%
    Echo PlayerLevel    = %PlayerLevel%
    Echo=
    Echo [Inventory]
    Echo PlayerSlotOne  = %PlayerSlotOne%
    Echo PlrSlot1Amnt   = %PlrSlot1Amnt%
)>"SaveGame.sav"

If you copy the following to your main batch file:

:LoadSection
If Not Exist "%~2" GoTo :EOF
Set "#="
For /F "Delims=:" %%A In ('"FindStr /BINC:"[%~1]" "%~2""') Do Set "#=%%A"
If Not Defined # GoTo :EOF
For /F "Tokens=1* Delims==" %%A In ('More +%#% "%~2"') Do (
    Echo=%%A|FindStr /BC:"[">Nul && (For %%C In (# $A $B) Do Set "%%C="
        GoTo :EOF)
    Set "$A=" & Call :Isolate %%A & Call :Isolate %%B
    Call Set "%%$A%%=%%$B%%")
GoTo :EOF

:Isolate
If Not Defined $A (Set "$A=%*") Else Set "$B=%*"
GoTo :EOF

You should then be able to call load the variables from a specific section like this:

Call :LoadSection "Inventory" "SaveGame.sav"

The doublequotes aren't needed in this particular case, as there are no characters to preserve in the strings, but I use them as best practice habit.

Compo
  • 36,585
  • 5
  • 27
  • 39
  • Thank you for the response! Great help! – Faraday92 Apr 14 '18 at 05:51
  • @Faraday92, if you feel that this answer solved your question better than the duplicate question's answers, and if the option is still available to you, you may click the green outlined checkmark to the left of the answer. This marks the answer as accepted and registers it on the site as 'has an accepted answer'. – Compo Apr 14 '18 at 14:51
  • Got it. Thanks for your help compo. – Faraday92 Apr 29 '18 at 19:38