将批处理变量导出到文本文件 [英] Export batch variables to text file

查看:108
本文介绍了将批处理变量导出到文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我被建议使用以下方法将变量从批处理脚本导出到文本文件中

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

set $>savefile

并在使用

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
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
: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

加载脚本

::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.

奖金问题:如果我对原始变量使用$ Variable,是否仍使用%Variable%调用它们?如果是这样,它将减少很多混乱.

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.

-我意识到批处理是编写脚本的次优语言,但它是我唯一熟悉的一种.我目前正在研究lua和C ++,并计划稍后通过转换批处理文件来实践两者.

--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.

文件已创建,但仅在第1行中包含$,并且仅包含其他内容.我认为我误解了变量导出命令的使用.

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.

推荐答案

如果您将%test%作为变量,这很简单.(但这可能是%%的任何事情),然后应该执行以下操作:

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

这篇关于将批处理变量导出到文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆