Reputation: 43
Okay so I have a batch program that writes my character's stats to a txt file when I save the game. However I am unable to load those stats properly. I thought maybe if I saved them with console commands and syntax properly that I could use the type command and it would run the commands setting the variables on the console to the ones in the txt file. Is there a simple way just like writing all of the variables to the txt file but reverse?
Sample code of variables being written:
if not exist "%~dp0\Users" md "Users" (
Echo set CharName=%UserCharacter%
Echo set Level=%Level%
Echo set Health=%Health%
Echo set ExpCap=%ExpCap%
)> "%~dp0\Users\%UserCharacter%.txt"
See I wrote it like this to the text file thinking I could load the file and it would set the variables to what the file had.
Didn't work. If you have any questions ask. I can't post pictures right now but I think I got my point across.
Upvotes: 1
Views: 1904
Reputation: 14325
You're really close. Instead of type
, run the text file through a for loop.
for /f "usebackq delims=" %%A in ("%~dp0\Users\%UserCharacter%.txt") do %%A
The set
commands you have will automatically run.
How It Works
A for /f
loop takes in a command, file, or string (in this case, a file) and processes the output of that thing line by line.
The usebackq
tells the for
loop that the thing in quotes is a file - ordinarily, a quoted string passed into a for /f
loop would be treated as a string instead, but since you have a space in your path, that needs to be quoted so that the loop doesn't throw an error.
delims=
tells the loop not to delimit (split) each line in the file. Ordinarily, a line is split on whitespace - spaces and tabs.
%%A
is the variable containing the first delimited token in the string. However, since we told the loop not to delimit the string, %%A
contains the entire string.
"%~dp0\Users\%UserCharacter%.txt"
is your file.
do
tells the loop that everything after this for the rest of the code block (either the rest of the line or anything between a (
on that line and the first )
on a lower line) should be performed while the loop is still ongoing.
%%A
in this case is the set
command you have stored in your text file. Because of how batch replaces and evaluates text, it is actually running the commands you have stored!
Upvotes: 1
Reputation: 540
You can save your variables with the following batch file:
@Echo off
if not exist "Users" md "Users"
if exist Users\%UserCharacter%.bat del Users\%UserCharacter%.bat
>>Users\%UserCharacter%.bat Echo @Echo off
>>Users\%UserCharacter%.bat Echo set CharName=%UserCharacter%
>>Users\%UserCharacter%.bat Echo set Level=%Level%
>>Users\%UserCharacter%.bat Echo set Health=%Health%
>>Users\%UserCharacter%.bat Echo set ExpCap=%ExpCap%
>>Users\%UserCharacter%.bat Echo Echo Variables loaded
Echo Variables saved
This creates a batch file named %UserCharacter%.bat in the Users subfolder. And if, for instance, your character name is Virtusal, you would then load those variables back by typing Users\Virtusal.bat.
Upvotes: 0