Export batch variables to text file

泄露秘密 提交于 2021-02-16 20:49:28

问题


I was recently given the advice to export variables from a batch script to a text file using

set $>savefile

and returning said variables upon another startup using

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

In implementation, this is not creating a file with the variables, resulting in error in the script, which closes the batch. To remedy the closing, I created a check to display an error message, but still cannot get the script to export variables into a file.

Variables

::Variables
set Location=Und
set Name=Und
set Gender=Und
set Age=Und
set Gold=0
set Hunger=Satisfied
set Illness=None
set Wounds=None
set CHP=10
set MHP=10
set CMP=0
set MMP=0
set DMG=(%STR%/5)+%RTWPDMG%
set DFN=0+%HeadAR%+%NeckAR%+%Shoulder%+%Chest%+%Glove%+%Leg%+%Feet%
set INT=1
set DEX=1
set STR=1
set Head=----
set Shoulder=----
set Neck=----
set Chest=Shirt
set Glove=----
set Leg=Pants
set Feet=Shoes
set LTWP=----
set RTWP=----
goto Start

Save script

::Save
:Save
cls
set $Location=%Location%
set $Name=%Name%
set $Gender=%Gender%
set $Age=%Age%
set $Gold=%Gold%
set $Hunger=%Hunger%
set $Illness=%Illness%
set $Wounds=%Wounds%
set $CHP=%CHP%
set $MHP=%MHP%
set $CMP=%CMP%
set $MMP=%MMP%
set $DMG=%DMG%
set $DFN=%DFN%
set $INT=%INT%
set $DEX=%DEX%
set $STR=%STR%
set $Head=%Head%
set $Shoulder=%Shoulder%
set $Neck=%Neck%
set $Chest=%Chest%
set $Glove=%Glove%
set $Leg=%Leg%
set $Feet=%Feet%
set $LTWP=%LTWP%
set $RTWP=%RTWP%
set $>%Name%_Savefile.txt
cls
echo SAVED
echo.
echo.
pause
Goto Start

And the load script

::Select Character to Load
:LoadError
Echo INVALID NAME
echo.
echo.
echo.
echo.
goto LoadSelect1
:LoadSelect
cls
goto LoadSelect1
:LoadSelect1
set /p Name=Enter character to load:
goto Load
:Load
cls
if EXIST %Name%_Savefile.txt goto LoadSuccess
goto LoadError
:Load
for /f "delims=" %%a in %Name%_Savefile.txt do set %%a
goto Stats

Am I missing something? I'd like all variables beginning with $ to be targeted without needing to list each one.

Bonus Question: If I use $Variable for the original variables, are they still called using %Variable%? If so, it'd cut down quite a bit of clutter.

--I realize batch is a sub-optimal language to script in, but it is the only one I am familiar with. I am currently working on lua and C++, and plan on practicing the two by converting the batch file later.

EDIT: The file is created, but only contains $ in line 1 and nothing else. I assume I have misunderstood the use of the variable export command.


回答1:


It is quite simple say if you had %test% as your variable. (But it can be %% any thing) You should then do this:

echo %test%
echo %test% >nameofyourtextfile.txt

Then to read the you should use:

for /f "delims=" %%x in (nameofyourtextfile.txt) do set "test=%%x"
echo %test%

works for the first line of the text file and >> sends the variable to last line. So you could use these like so:

echo %test1% > nameofyourtextfile.txt
echo %test2% >> nameofyourtextfile.txt
echo %test3% >> nameofyourtextfile.txt

To load from different lines you would use:

for /F "tokens=1,2,3" %%i in (nameofyourtextfile.txt) do call :process %%i %%j %%k
goto thenextstep
:process
set test1=%1
set test2=%2
set test3=%3

You could add more tokens like so:

for /F "tokens=1,2,3,4,5" %%i in (nameofyourtextfile.txt) do call :process %%i %%j %%k
goto thenextstep
:process
set test1=%1
set test2=%2
set test3=%3
set test4=%4
set test5=%5



回答2:


If you do it the way you posted, what you have is (just a extract)

in your main code set Wounds=None

in your save code set $Wounds=%Wounds% set $>filename

in your load code for %%a .... set %%a that gets translated to set $Wounds=None

At the end, you have not assigned a loaded value to %Wounds%, so an aditional set Wounds=%$Wounds% is needed.

I think the advice on using $ you received was an advice on using a common prefix to all the variables you want to save, so, you can send them and load them from a file with little effort. You can use $ or whatever other thing you want as long as it is a valid sintax and you change all the variables that you want save/load to that sintax.

By example

    call :initializeCharacter
    .... 

    call :save

    ....

    call :load 

:initializeCharacter
    set "ch.Name=Und"
    set "ch.Gender=Und"
    ....
    exit /b

:save 
    set ch.>"c:\savedGames\save.txt"
    exit /b

:load
    for /f "usebackq delims=" %%a in ("c:\savedGames\save.txt") do set "%%a"
    exit /b 

When the load function gets called, the variables (named ch.whatever and saved as ch.whatever) get overwritten on file read. That way, the variables in your code, what you save and what you load, all, have the same names. No need to set $myVar=%myVar% on save or set myVar=%$myVar%on load

And use $, ch., .... or whatever prefix you prefer in all the variables that needs to be saved/loaded in a block. What you don't want to save/load should have names that do not match the prefix used in the set prefix>file. Better option is to use different prefixes for different domain/level/conceptual element/...



来源:https://stackoverflow.com/questions/23008693/export-batch-variables-to-text-file

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