1

我想做一个这样的菜单:

mode 59, 300
set line===========================================================
echo %line%
echo         Tool colour menu!
echo %line%
Echo.
echo     Choose your BACKGROUND colour!
echo.
echo         1  =   Blue        
echo         2  =   Green       
echo         3  =   Aqua        
echo         4  =   Red     
echo         5  =   Purple      
echo         6  =   Yellow      
echo         7  =   White
echo         8  =   Gray
echo         9  =   Light Blue
echo         10 =   Light Green
echo         11 =   Light Aqua
echo         12 =   Light Red
echo         13 =   Light Purple
echo         14 =   Light Yellow
echo         15 =   Bright White
echo.
set /p background_app=Enter the number of the colour that you want for background (or enter for default):

所以那个菜单就是 background_app 变量!

接着:

mode 59, 300
set line===========================================================
echo %line%
echo         Tool color menu!
echo %line%
Echo.
echo     Choose your TEXT color!
echo.
echo         1  =   Blue        
echo         2  =   Green       
echo         3  =   Aqua        
echo         4  =   Red     
echo         5  =   Purple      
echo         6  =   Yellow      
echo         7  =   White
echo         8  =   Gray
echo         9  =   Light Blue
echo         10 =   Light Green
echo         11 =   Light Aqua
echo         12 =   Light Red
echo         13 =   Light Purple
echo         14 =   Light Yellow
echo         15 =   Bright White
echo.
set /p text_app=Enter the number of the color that you want for text (or enter for default):

这将是变量 %text_app%

用户输入后,我想将此变量保存在 txt 文件中,以便稍后检索这些值,以防用户运行该工具(以保留该人选择的颜色)

但目前我已经尝试过:

:saveVars
(
ECHO Backuground=%background_app%
ECHO Text=%text_app%
) >colors.txt
GOTO :EOF

例如,它将像这样保存:

Background=1
Text=7

现在进退两难了,因为我只想从 colors.txt 中读取值并将其设置为变量:

%background_apptxt%
%text_apptxt%

我怎样才能只读取背景和文本的值?谢谢你的帮助 :)


好吧,理解 FOR 很容易,但例如:

Colors.txt 包含以下行:

Background=1
Text=2

所以例如我用这个批次创建了一个测试批次:

@echo off
set background_app=black
set text_app=green

:saveVars
(
ECHO Background=%background_app%
ECHO Text=%text_app%
) >colors.txt

for /f "tokens=1,2 delims==" %%A in (colors.txt) do set "%%A_app=%%B"

如何回显 %background_app% 并再次回显 %text_app% ?谢谢

4

2 回答 2

1

使用 FOR /F 循环来读取和解析每一行。您可以将令牌分隔符设置为,=以便获得 2 个令牌。

for /f "tokens=1,2 delims==" %%A in (colors.txt) do set "%%A_app=%%B"

如果您的文本文件包含每个变量的全名,则代码可能会更容易。然后你可以简单地使用

for /f "delims=" %%A in (colors.txt) do set "%%A"
于 2013-02-07T05:18:29.103 回答
0

很好地找到了另一种保存和读取 ini 的方法:

http://www.horstmuc.de/wbat32.htm#inifile

希望有人和我一样觉得这很有用:)

于 2013-02-07T19:07:54.723 回答