How to get a file Variables in Batch-File

♀尐吖头ヾ 提交于 2019-12-13 03:25:13

问题


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


来源:https://stackoverflow.com/questions/50632102/how-to-get-a-file-variables-in-batch-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!