Search code examples
variablesbatch-filesavecalltoken

Retrieving variables from .txt error; Batch


:LoadSelect1
set /p Name=Enter character to load:
cls
if EXIST "%Name%_Savefile.txt" goto Load
goto LoadError
:Load
for /f "tokens=1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26" %%a in (%Name%_Savefile.txt) do call :Process %%a %%b %%c %%d %%e %%f %%g %%h %%i %%j %%k %%l %%m %%n %%o %%p %%q %%r %%s %%t %%u %%v %%w %%x %%y %%z
goto Stats
:Process
set Location=%1
set Name=%2
set Gender=%3
set Age=%4
set Gold=%5
set Hunger=%6
set Illness=%7
set Wounds=%8
set CHP=%9
set MHP=%10
set CMP=%11
set MMP=%12
set DMG=%13
set DFN=%14
set INT=%15
set DEX=%16
set STR=%17
set Head=%18
set Shoulder=%19
set Neck=%20
set Chest=%21
set Glove=%22
set Leg=%23
set Feet=%24
set LTWP=%25
set RTWP=%26

Above code is used to retrieve variables saved to a .txt file in my batch script. Variables save fine to individual lines. When Call :Process function is used, %1 is applied for %1, and "%1X" become %1 with number attached. Meanwhile %2-9 are blank, and %2X are blanks with number attached. Solution would be great.

Pastebin of full code

To recreate: Run as batch file. Go through prompts until section with 9 choices. Type "Save". Close and reopen. Go to Load option. Type in name from previous. View errors.


Solution

  • It would be easier if you save your savefile.txt in this format :

    savefile.txt

    Location=AAAA
    Name=BBBB
    Gender=CCCC
    Age=DDDD
    Gold=EEEE
    Hunger=FFFF
    Illness=GGGG
    Wounds=HHHH
    

    and then to set the variables saved in savefile.txt :

    for /f "delims=" %%a in (savefile.txt) do set %%a
    

    You can also save your savefile as .bat then it will look like this :

    set Location=AAAA
    set Name=BBBB
    ----
    

    and with the call:savefile.bat all your Vars will be evaluated