0

Im trying to change the %%A value after using Findstr to locate the first matched string. %%A is located in a separate file and after looking over multiple other posts, i cant figure out a solution. I am relatively new to batch scripting but im an extremely fast learner when i can find tutorials or explanations that help me connect two things. Code shown below.

for /f "delims=" %%A in ('findstr "nila" Players\%name1%.bat') do ( 
     Echo %%A
     Set %%A=%%A:nila=Bronze Dagger%
     Echo %%A
     pause
     goto Shop1
)
Buggy
  • 1

2 Answers2

1
setlocal enabledelayedexpansion
for /f "delims=" %%A in ('findstr "nila" Players\%name1%.bat') do ( 
     Echo %%A
 set "string=%%A"
 Set "string=!string:nila=Bronze Dagger!"
     Echo !string!
     pause
     goto Shop1
)

Such operations cannot be performed directly on a metavariable such as %%A but need to be manipulated through an ordinary user-variable.

Note that since the value of string is changing within a code block (parenthesised sequence of lines) then you need to use delayedexpansion and !var! to access the changed value.

see Stephan's DELAYEDEXPANSION link

Use set "var=value" for setting string values - this avoids problems caused by trailing spaces. Don't assign a terminal backslash, Space or " - build pathnames from the elements - counterintuitively, it is likely to make the process easier

Tip for game-generation:

If you reserve a character as a prefix for variables-you-want-to-save (eg all variables I want to save/reload start with #) then all you need to save a game is

set #>"mygamefile.txt"

and all you need to reload a game is

for /f "usebackqdelims=" %%a in ("mygamefile.txt") do set "%%a"

To zap all # variables (useful before reloading a game) use

for /f "delims==" %%a in ('set # 2^>nul') do set "%%a="
Magoo
  • 77,302
  • 8
  • 62
  • 84
  • So i tried it and it worked, kinda. What i am trying to do is create an inventory system that is recorded onto the player's save file, but i dont know how to change the value of the inventory slot. do you want me to send the save file code? the !string! was changed but the original save value wasnt – Buggy Jun 19 '22 at 19:00
  • Would i be able to make a function with the save file that edits/modifies values within it? Or would that just make it more complicated – Buggy Jun 19 '22 at 19:02
  • You may get some ideas from this question https://stackoverflow.com/q/72672485/2128947 – Magoo Jun 19 '22 at 19:05
  • I looked it over but nothing really came to mind. My inventory is included with the entire save file. though i do know the exact lines that the inventory resides within 56-83 but im pretty confused on alot of things since im new to batch scripting. – Buggy Jun 19 '22 at 19:33
-1

Resolved the issue by splitting data file into separate files and making a load function to load the variable and then change it before saving again.

Buggy
  • 1
  • 2
    Thus requires further explanation, to be considered as a functional solution to the posed question. Where is your code, or at least sufficient information for future readers to utilize your methodology? – Compo Jun 19 '22 at 22:34
  • Will post an edit with code when im back home. – Buggy Jun 19 '22 at 22:56