How to get a file Variables in Batch-File

2019-08-16 07:12发布

问题:

Let's imagine a Batch game saves your progress in a file.bat file, when we edit that file we got :

set Health=100
set Sword=Gold

i used this command in a Batch file but it's didn't work :

for /f "eol=- delims=" %%a in (file.bat) do set "%%a"
echo Your health is %Health%
echo Your sword is made of %Sword%

it leave it blank.

but if i maked a file like the above but removed the set (only Health=100 and Sword=Gold without set), it works and give me the variable Health and Sword, but when the set command added it didn't work, if any one knows solution please tell me and Thanks !

I want a command that give me the Variables i don't want to just delete the set command from the file, because if i did that the game that saved the bat file will get corrupted while extracting the Variables from the file to continue where you left the game.

回答1:

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="