批量变量
Variables in batch
我正在尝试创建一个代码,以这样的方式基于选择来处理变量:
set item1=ring
set item2=deadhead
set item3=bloodstainedmap
set item4=strangebox
cls
echo 1) %item1%
echo 2) %item2%
echo 3) %item3%
echo 4) %item4%
echo.
echo Which Item do you wish to look at?
choice /c 1234 /n >nul
if %errorlevel%==1 goto itemin
if %errorlevel%==2 goto itemin
if %errorlevel%==3 goto itemin
if %errorlevel%==4 goto itemin
:itemin
cls
echo %item%errorlevel%%
::^^^^ this part here should read "ring" if i selected 1. It reads "errorlevel"
显然,我希望项目在加载变量时能够正确显示....
有什么帮助吗?
A.S.C
这是我对此任务的建议:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "item1=ring"
set "item2=deadhead"
set "item3=bloodstainedmap"
set "item4=strangebox"
cls
echo 1) %item1%
echo 2) %item2%
echo 3) %item3%
echo 4) %item4%
echo/
setlocal EnableDelayedExpansion
:UserChoice
%SystemRoot%\System32\choice.exe /C 1234 /N /M "Which item do you wish to look at?"
if not errorlevel 1 goto UserChoice
set "item=!item%ERRORLEVEL%!"
endlocal & set "item=%item%"
echo %item%
endlocal
如果用户按 Ctrl+C 并按 cmd.exe
回答提示以退出处理 的批处理文件,则会重复用户提示N 在这种情况下,条件 if not errorlevel 1
为真,因为在此特殊用例中,CHOICE 的退出代码为 0
。
当然也可以在批处理文件的顶部启用 delayed expansion。必须考虑到所有行在这样做时都被解析了两次,并且 !
在任何地方都被解释为延迟扩展环境变量引用的 start/end。
为了了解使用的命令及其工作原理,请打开 command prompt window,在其中执行以下命令,并仔细阅读为每个命令显示的所有帮助页面。
choice /?
cls /?
echo /?
endlocal /?
if /?
set /?
setlocal /?
另请参阅:
- How does the Windows Command Interpreter (CMD.EXE) parse scripts?
- Single line with multiple commands using Windows batch file
- Why is no string output with 'echo %var%' after using 'set var = text' on command line?
- DosTips 论坛主题:ECHO. FAILS to give text or blank line - Instead use ECHO/
作为@Mofi's 的补充:
Choice 命令可以在 for /F 循环中执行,允许直接使用 choice 选项而不是评估错误级别。必须使用 /N 无提示开关,必须排除使用 /M 自定义提示开关。
获取实际按下的键而不是错误级别的语法是:
@ECHO Off & Goto :main
====================================
::: Script Break - Functions :::
====================================
====================================
:main :::
====================================
::: Macro Definitions :::
====================================
(Set \n=^^^
%= Newline var \n for macro definition - Do not modify. =%)
If "!![" == "[" (
Echo/DelayedExpansion Not permitted prior to Macro Definition.
Exit /B 0
)
========================================== ::: MENU macro prep and Definition :::
rem /* Key index list Allows 36 menu options. Component of Menu Macro /*
Set "ChoList=0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
rem /* Menu macro Usage: %Menu% "quoted" "list of" "options"
==========================================
==== Set Menu=For %%n in (1 2)Do if %%n==2 (%\n%
%= Output Dividing Line =% Echo(!DIV!%\n%
%= Reset CH# index value for Opt[#] =% Set "CH#=0"%\n%
%= Undefine choice option key list =% Set "CHCS="%\n%
%= For Each in list; =% For %%G in (!Options!)Do (%\n%
%= For Option Index value =% For %%i in (!CH#!)Do (%\n%
%= Build the Choice key list and Opt[#] =% Set "CHCS=!CHCS!!ChoList:~%%i,1!"%\n%
%= array using the character at the =% Set "Opt[!ChoList:~%%i,1!]=%%~G"%\n%
%= current substring index. Display. =% Echo([!ChoList:~%%i,1!] %%~G%\n%
%= Increment Opt[#] Index var CH# =% Set /A "CH#+=1"%\n%
%= Close CH# loop =% )%\n%
%= Close Options loop =% )%\n%
%= Output Dividing Line =% Echo(!DIV!%\n%
%= Select option by character index =% For /F "Delims=" %%o in ('Choice /N /C:!CHCS!')Do (%\n%
%= Assign return var OPTION with the =% Set "Option=!Opt[%%o]!"%\n%
%= value selected from Opt[CH#] array. =% )%\n%
%= Capture Macro input - Options List =% )Else Set Options=
========================================== ::: End Menu Definition :::
rem /* Get console width for dividing line. */
for /F "usebackq tokens=2* delims=: " %%W in (`mode con ^| findstr Columns`) do Set /A "Console_Width=%%W"
rem /* Enable environment for macro expansion */
Setlocal EnableDelayedExpansion
rem /* Build dividing line. */
Set "DIV="&For /L %%i in (2 1 %Console_Width%)Do Set "DIV=!DIV!-"
====================================
::: Script Body :::
====================================
rem /* Optional - Define an Options list. */
Set "Loot[1]= "ring" "dead head" "blood stained map" "strange box""
:menu
CLS & Title %~n0
%Menu% "exit" %Loot[1]%
If "!Option!" == "exit" (Endlocal & Goto :Eof)Else Echo/You chose !Option!
rem /* Optional - remove selected option from list of options. Options should be Doublequoted with leading whitespace. */
Set "Loot[1]=!Loot[1]: "%Option%"=!"
rem /* Do whatever you want with your option here. Execute it, use options as label names to call etc. */
Pause
Goto :menu
我正在尝试创建一个代码,以这样的方式基于选择来处理变量:
set item1=ring
set item2=deadhead
set item3=bloodstainedmap
set item4=strangebox
cls
echo 1) %item1%
echo 2) %item2%
echo 3) %item3%
echo 4) %item4%
echo.
echo Which Item do you wish to look at?
choice /c 1234 /n >nul
if %errorlevel%==1 goto itemin
if %errorlevel%==2 goto itemin
if %errorlevel%==3 goto itemin
if %errorlevel%==4 goto itemin
:itemin
cls
echo %item%errorlevel%%
::^^^^ this part here should read "ring" if i selected 1. It reads "errorlevel"
显然,我希望项目在加载变量时能够正确显示....
有什么帮助吗?
A.S.C
这是我对此任务的建议:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "item1=ring"
set "item2=deadhead"
set "item3=bloodstainedmap"
set "item4=strangebox"
cls
echo 1) %item1%
echo 2) %item2%
echo 3) %item3%
echo 4) %item4%
echo/
setlocal EnableDelayedExpansion
:UserChoice
%SystemRoot%\System32\choice.exe /C 1234 /N /M "Which item do you wish to look at?"
if not errorlevel 1 goto UserChoice
set "item=!item%ERRORLEVEL%!"
endlocal & set "item=%item%"
echo %item%
endlocal
如果用户按 Ctrl+C 并按 cmd.exe
回答提示以退出处理 的批处理文件,则会重复用户提示N 在这种情况下,条件 if not errorlevel 1
为真,因为在此特殊用例中,CHOICE 的退出代码为 0
。
当然也可以在批处理文件的顶部启用 delayed expansion。必须考虑到所有行在这样做时都被解析了两次,并且 !
在任何地方都被解释为延迟扩展环境变量引用的 start/end。
为了了解使用的命令及其工作原理,请打开 command prompt window,在其中执行以下命令,并仔细阅读为每个命令显示的所有帮助页面。
choice /?
cls /?
echo /?
endlocal /?
if /?
set /?
setlocal /?
另请参阅:
- How does the Windows Command Interpreter (CMD.EXE) parse scripts?
- Single line with multiple commands using Windows batch file
- Why is no string output with 'echo %var%' after using 'set var = text' on command line?
- DosTips 论坛主题:ECHO. FAILS to give text or blank line - Instead use ECHO/
作为@Mofi's
Choice 命令可以在 for /F 循环中执行,允许直接使用 choice 选项而不是评估错误级别。必须使用 /N 无提示开关,必须排除使用 /M 自定义提示开关。
获取实际按下的键而不是错误级别的语法是:
@ECHO Off & Goto :main
====================================
::: Script Break - Functions :::
====================================
====================================
:main :::
====================================
::: Macro Definitions :::
====================================
(Set \n=^^^
%= Newline var \n for macro definition - Do not modify. =%)
If "!![" == "[" (
Echo/DelayedExpansion Not permitted prior to Macro Definition.
Exit /B 0
)
========================================== ::: MENU macro prep and Definition :::
rem /* Key index list Allows 36 menu options. Component of Menu Macro /*
Set "ChoList=0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
rem /* Menu macro Usage: %Menu% "quoted" "list of" "options"
==========================================
==== Set Menu=For %%n in (1 2)Do if %%n==2 (%\n%
%= Output Dividing Line =% Echo(!DIV!%\n%
%= Reset CH# index value for Opt[#] =% Set "CH#=0"%\n%
%= Undefine choice option key list =% Set "CHCS="%\n%
%= For Each in list; =% For %%G in (!Options!)Do (%\n%
%= For Option Index value =% For %%i in (!CH#!)Do (%\n%
%= Build the Choice key list and Opt[#] =% Set "CHCS=!CHCS!!ChoList:~%%i,1!"%\n%
%= array using the character at the =% Set "Opt[!ChoList:~%%i,1!]=%%~G"%\n%
%= current substring index. Display. =% Echo([!ChoList:~%%i,1!] %%~G%\n%
%= Increment Opt[#] Index var CH# =% Set /A "CH#+=1"%\n%
%= Close CH# loop =% )%\n%
%= Close Options loop =% )%\n%
%= Output Dividing Line =% Echo(!DIV!%\n%
%= Select option by character index =% For /F "Delims=" %%o in ('Choice /N /C:!CHCS!')Do (%\n%
%= Assign return var OPTION with the =% Set "Option=!Opt[%%o]!"%\n%
%= value selected from Opt[CH#] array. =% )%\n%
%= Capture Macro input - Options List =% )Else Set Options=
========================================== ::: End Menu Definition :::
rem /* Get console width for dividing line. */
for /F "usebackq tokens=2* delims=: " %%W in (`mode con ^| findstr Columns`) do Set /A "Console_Width=%%W"
rem /* Enable environment for macro expansion */
Setlocal EnableDelayedExpansion
rem /* Build dividing line. */
Set "DIV="&For /L %%i in (2 1 %Console_Width%)Do Set "DIV=!DIV!-"
====================================
::: Script Body :::
====================================
rem /* Optional - Define an Options list. */
Set "Loot[1]= "ring" "dead head" "blood stained map" "strange box""
:menu
CLS & Title %~n0
%Menu% "exit" %Loot[1]%
If "!Option!" == "exit" (Endlocal & Goto :Eof)Else Echo/You chose !Option!
rem /* Optional - remove selected option from list of options. Options should be Doublequoted with leading whitespace. */
Set "Loot[1]=!Loot[1]: "%Option%"=!"
rem /* Do whatever you want with your option here. Execute it, use options as label names to call etc. */
Pause
Goto :menu