我想做一个这样的菜单:
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_appvariables!
接着:
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):
这将是variables%text_app%
用户input后,我想将这些variables保存在一个txt文件中,以便以后可以检索这些值,以防万一用户运行该工具(保持该用户select的颜色)
但是目前我已经尝试过了:
:saveVars ( ECHO Backuground=%background_app% ECHO Text=%text_app% ) >colors.txt GOTO :EOF
比如它会保存这样的:
Background=1 Text=7
现在出现这个困境,因为我想只读取colors.txt中的值,并将其设置为如下variables:
%background_apptxt% %text_apptxt%
我怎样才能读取背景和文本的价值? 谢谢你的帮助 :)
那很容易理解FOR,但是例如:
Colors.txt包含以下几行:
Background=1 Text=2
所以例如我用这个批处理创build一个testing批处理:
@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%? 谢谢
使用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"