1

I have made a character file in which my game pulls data and variables from. Looks like so:

playerName= Marche
playerRace= Elf
playerHP= 100
playerSPD= 200
playerATK= 120
playerDEF= 70

Final Fantasy reference anyone...? Anyway, when the character levels up, I need a batch script to find the string "playerHP= 100". playerHP is set as a variable within the script. Basically, it takes the current health, and multiplies it by 120%, increasing the number. How do I echo the result of that math to replace the current number?

For example if that didn't make any sense, I have 100 health. I level up, thus increasing my health stat by 120%, so now I have 120 health. I would want to find the string "playerHP= 100" and replace it with "playerHP= 120".

If it can be avoided I don't want to download any other commands (I've seen sed a few times). Thanks much

EDIT: Instead of searching for the string and replacing I took jeb's advice and just deleted the file and re-echoed all of the data. It ended up looking like this:

set /a LeveledUpPlayerHP=(%ppHP%* 12) / (10)
set /a LeveledUpPlayerSPD=(%ppSPD%* 12) / (10)
set /a LeveledUpPlayerATK=(%ppATK%* 12) / (10)
set /a LeveledUpPlayerDEF=(%ppDEF%* 12) / (10)
echo Updating stats...
del "C:\Users\%USERNAME%\Desktop\CMDRPG\player\playerData.dll
ping 1.1.1.1 -n 1 -w 500 > nul
echo playerName= %playerName%>playerData.dll
echo playerRace= %playerRace%>>playerData.dll
echo playerHP= %LeveledUpPlayerHP%>>playerData.dll
echo playerSPD= %LeveledUpPlayerSPD%>>playerData.dll
echo playerATK= %LeveledUpPlayerATK%>>playerData.dll
echo playerDEF= %LeveledUpPlayerDEF%>>playerData.dll

The playerName and playerRace are all loaded in prior to this section of the code. Ping is used just as a wait function to let the file delete before echoing new data. Seems to work okay. Thanks all

user2654489
  • 85
  • 1
  • 1
  • 4
  • possible duplicate of [Batch Script to replace line in an .ini](http://stackoverflow.com/questions/17800581/batch-script-to-replace-line-in-an-ini) – jeb Aug 06 '13 at 16:15
  • Also try a look at [SO:How do you make a save/load cmd for a batch rpg?](http://stackoverflow.com/q/17081050/463115) – jeb Aug 06 '13 at 16:20
  • I looked at them, still a bit confused. I COULD just delete the file and output the new information instead of finding and replacing. I was not sure if there was a simple command that lets me do so. Is there a way to replace a certain line in a text file with new information? That's really all I need – user2654489 Aug 06 '13 at 16:22
  • Obviously replacing a single line in a file needs also to replace the complete file. So it's easier to rewrite the complete file each time – jeb Aug 06 '13 at 16:27
  • Thats what I figured. Since it's only a few lines it shouldn't be a big deal. Thanks for the help – user2654489 Aug 06 '13 at 16:35

3 Answers3

2

try this (output is in %inifile%.new):

@ECHO OFF &SETLOCAL ENABLEDELAYEDEXPANSION
SET "inifile=file"

FOR /f %%a IN ('^<"%inifile%" find /c /v ""') DO SET /a lines=%%a
< "%inifile%" (
FOR /l %%a IN (1,1,%lines%) DO (
    SET "line="
    SET /p "line="
    IF NOT "!line:playerHP=!"=="!line!" (
        FOR /f "tokens=2delims= " %%b IN ("!line!") DO SET /a HP=%%b*12/10
        SET "line=playerHP= !HP!"
    )
    ECHO(!line!
))>"%inifile%.new"

Input/output:

>type file
playerName= Marche
playerRace= Elf
playerHP= 100
playerSPD= 200
playerATK= 120
playerDEF= 70

>test.bat

>type file.new
playerName= Marche
playerRace= Elf
playerHP= 120
playerSPD= 200
playerATK= 120
playerDEF= 70
Endoro
  • 37,015
  • 8
  • 50
  • 63
2

Presumeably it does not matter what order the values appear in you file. If that is so, then the following will effectively "edit" your file.

It first uses FINDSTR to isolate the current playerHP value, and FOR /F to parse out the value from the string. SET /A increments the playerHP. Then a new file is created using FINDSTR to write all the current values except for playerHP, and then the new playerHP info is appended to the end.

@echo off
set "file=gameData.txt"
for /f "tokens=2 delims==" %%N in ('findstr /bl "playerHP=" "%file%"') do set /a "newHP=%%N*120/100"
>"%file%.mod" (
  findstr /blv "playerHP=" "%file%"
  echo playerHP=%newHP%
)
move /y "%file%.mod" "%file%" >nul

But why go to all that trouble. I should think you already have all your values in memory as environment variables. Simply make sure that all variables that are to be stored in a file start with a common unique prefix. In your example, they all start with "player", but you probably want something a little more generic - perhaps a symbol like #. So you could have #playerName, #playerRace, etc.

Simply update the values in memory as needed. When it comes time to save the current game state to a file, then all you need to do is

>"gameData.txt" set #

A brand new file will be created each time containing all the current values.

To load the values, you do exactly what jeb suggested:

for /f "usebackq delims=" %%A in ("gameData.txt") do set %%A
dbenham
  • 127,446
  • 28
  • 251
  • 390
1

To save your data in a file, you could use a block like

(
  echo playerName=%playerName%
  echo playerRace=%playerRace%
  echo playerHP=%playerHP%
) > player.ini

And to load it you could use

for /F "delims=" %%L in (player.ini) do set "%%L"
jeb
  • 78,592
  • 17
  • 171
  • 225