批处理文件错误“此时 /100 是意外的”
Batch file error "/100 was unexpected at this time"
我用批处理脚本做了一个有趣的游戏,但在这段代码中我有一条错误消息“/100 unexpected at this time”我真的不明白为什么?请帮帮我!!
@echo off
mode con cols=110 lines=32
setlocal enabledelayedexpansion
set npctier=0
goto randomnpc
:randomnpc
if %npctier% EQU 0 (
set npctype=Wooden Dummy
set /a npclvl=%random% %% 5+1
set /a npchp=%npclvl% * 100
set /a npcdmg=0
set /a npcdef=(%npchp%*5)/100
set /a npcxp=%npclvl%*100 )
:combatchoice
echo.
echo. You see %npctype% level %npclvl%.
echo.
echo. The %npctype%'s Health: %npchp% HP
echo.
goto main
我建议先打开一个command prompt,运行 set /?
,然后从第一页的顶部到最后一页的底部仔细完整地阅读输出帮助。有说明:
Any non-numeric strings in the expression are treated as environment variable names whose values are converted to numbers before using them. If an environment variable name is specified but is not defined in the current environment, then a value of zero is used. This allows you to do arithmetic with environment variable values without having to type all those % signs to get their values.
所以可以只写 set /A npchp=npclvl * 100
因为 npclvl
在算术表达式中被解释为环境变量的名称,命令行甚至在以 [= 开头的命令块中也能工作15=] 并以匹配 )
结束而不使用延迟环境变量扩展。
然后 运行 cmd /?
并从第一页的顶部到最后一页的底部仔细完整地再次阅读输出帮助。解释了包含 space 这些字符之一 &()[]{}^=;!'+,`~
的文件名(或任何其他参数字符串)必须包含在 "
中才能将这些字符解释为参数的文字字符字符串。
另请阅读How does the Windows Command Interpreter (CMD.EXE) parse scripts?
因此,命令行 set /a npcdef=(%npchp%*5)/100
应使用以下符号之一编写:
set /A npcdef=npchp*5/100
set /A npcdef=npchp * 5 / 100
set /A "npcdef=(npchp*5)/100"
set /A npcdef=(npchp*5^)/100
脱字符 ^
转义下一个字符以解释为文字字符,但下一个字符是 %
,必须用 %
.
转义
在编写输出空行的批处理文件时,还应考虑 DosTips 论坛主题 ECHO. FAILS to give text or blank line - Instead use ECHO/ 上发布的建议。
批处理文件的主要改进是更改 IF 条件,从而完全避免使用命令块。
@echo off
setlocal EnableExtensions DisableDelayedExpansion
%SystemRoot%\System32\mode.com con cols=110 lines=32
set npctier=0
set "npctype=Wooden Dummy"
:randomnpc
if %npctier% NEQ 0 goto CombatChoice
set /A npclvl=%random% %% 5 + 1
set /A npchp=npclvl*100
set npcdmg=0
set /A npcdef=npchp*5/100
set /A npcxp=npclvl*100
:CombatChoice
echo/
echo You see %npctype% level %npclvl%.
echo/
echo The %npctype%'s health: %npchp% HP
echo/
endlocal
另见 Why is no string output with 'echo %var%' after using 'set var = text' on command line? 它包含一些关于如何使用命令 SET 的提示,但未写入此命令的 help/documentation。
如果你想保持相同的结构,那么像这样的东西会更好:
@Echo Off
SetLocal EnableExtensions
%__APPDIR__%mode.com 110, 32
Set "npctier=0"
:randomnpc
If %npctier% Equ 0 (
Set "npctype=Wooden Dummy"
Set "npcdmg=0"
Set /A "npclvl=(%RANDOM% %% 5) + 1"
Set /A "npchp=npclvl * 100, npcdef=npclvl * 5, npcxp=npchp"
)
:combatchoice
Echo=
Echo You see %npctype% level %npclvl%.
Echo=
Echo The %npctype%'s Health: %npchp% HP
Echo=
GoTo main
您应该注意,可以在一个算术指令中使用 Set /A
定义多个值
在您因创建重复代码的冗长脚本而太得意忘形之前,我认为值得向您介绍批处理宏。
Batch 中的宏是分配给变量的命令或命令块,通过使用 For 循环结合 If / Else 条件,可以使用捕获参数允许变量用作函数。
下面的示例包含宏的两个变体,可用于轻松生成不同的遭遇类型,只需最少的重复脚本,同时还可避免使用低效的函数调用。
宏必须在启用延迟扩展之前定义,因为在宏执行期间引用的变量是使用 !
扩展定义的,以便宏在扩展时解析的值是当时变量的值解析,而不是定义期间的值。
@echo off
mode con cols=110 lines=32
(Set \n=^^^
%= macro newline DNR =%
)
(Set LF=^
%= Linefeed DNR =%)
rem Example 1: npc generation; Fixed Formula macro
rem USAGE: %npc{generic}%{Npc name / type}{npc max level}{npc multiplier}{damage value or formula}
Set npc{generic}=For %%n in (1 2)Do if %%n==2 (%\n%
For /F "Tokens=1,2,3,4 Delims={}" %%G in ("!Params!")Do (%\n%
Set "npctype=%%G" %\n%
Set /a "npclvl= !random! %% %%H + 1" %\n%
Set /a "npchp= !npclvl! * %%I" %\n%
Set /a "npcdmg= %%J" %\n%
Set /a "npcdef= ( !npchp! * %%H ) / %%I" %\n%
Set /a "npcxp=!npclvl! * %%I" %\n%
echo/Enemy: !npctype!!LF!level: !npclvl!!LF!HP: !npchp!!LF!Damage: !npcdmg!!LF!Defence: !npcdef!!LF!XP: !npcxp!!LF!%\n%
)%\n%
) Else Set Params=
rem Example 2: npc generation; Supplied Formula macro
rem USAGE: %npc{boss}%{Npc name / type}{level value or formula}{hp value or formula}{damage value or formula}{defense value or formula}{xp value or formula}
Set npc{boss}=For %%n in (1 2)Do if %%n==2 (%\n%
For /F "Tokens=1,2,3,4,5,6 Delims={}" %%G in ("!Params!")Do (%\n%
Set "npctype=%%G" %\n%
Set /a "npclvl= %%H " %\n%
Set /a "npchp= %%I " %\n%
Set /a "npcdmg= %%J " %\n%
Set /a "npcdef= %%K " %\n%
Set /a "npcxp= %%L " %\n%
echo/Enemy: !npctype!!LF!level: !npclvl!!LF!HP: !npchp!!LF!Damage: !npcdmg!!LF!Defence: !npcdef!!LF!XP: !npcxp!!LF!%\n%
)%\n%
) Else Set Params=
rem enable delayed expansion after macro definitions
setlocal enableextensions enabledelayedexpansion
:randomnpc
%npc{generic}%{Wooden Dummy}{5}{100}{0}
Pause
%npc{boss}%{Dragon}{!random! %% 10 + 10}{npclvl * 200}{npchp / 20}{npchp / (npclvl / 2)}{npclvl * 150}
Endlocal
Goto :Eof
我用批处理脚本做了一个有趣的游戏,但在这段代码中我有一条错误消息“/100 unexpected at this time”我真的不明白为什么?请帮帮我!!
@echo off
mode con cols=110 lines=32
setlocal enabledelayedexpansion
set npctier=0
goto randomnpc
:randomnpc
if %npctier% EQU 0 (
set npctype=Wooden Dummy
set /a npclvl=%random% %% 5+1
set /a npchp=%npclvl% * 100
set /a npcdmg=0
set /a npcdef=(%npchp%*5)/100
set /a npcxp=%npclvl%*100 )
:combatchoice
echo.
echo. You see %npctype% level %npclvl%.
echo.
echo. The %npctype%'s Health: %npchp% HP
echo.
goto main
我建议先打开一个command prompt,运行 set /?
,然后从第一页的顶部到最后一页的底部仔细完整地阅读输出帮助。有说明:
Any non-numeric strings in the expression are treated as environment variable names whose values are converted to numbers before using them. If an environment variable name is specified but is not defined in the current environment, then a value of zero is used. This allows you to do arithmetic with environment variable values without having to type all those % signs to get their values.
所以可以只写 set /A npchp=npclvl * 100
因为 npclvl
在算术表达式中被解释为环境变量的名称,命令行甚至在以 [= 开头的命令块中也能工作15=] 并以匹配 )
结束而不使用延迟环境变量扩展。
然后 运行 cmd /?
并从第一页的顶部到最后一页的底部仔细完整地再次阅读输出帮助。解释了包含 space 这些字符之一 &()[]{}^=;!'+,`~
的文件名(或任何其他参数字符串)必须包含在 "
中才能将这些字符解释为参数的文字字符字符串。
另请阅读How does the Windows Command Interpreter (CMD.EXE) parse scripts?
因此,命令行 set /a npcdef=(%npchp%*5)/100
应使用以下符号之一编写:
set /A npcdef=npchp*5/100
set /A npcdef=npchp * 5 / 100
set /A "npcdef=(npchp*5)/100"
set /A npcdef=(npchp*5^)/100
脱字符 ^
转义下一个字符以解释为文字字符,但下一个字符是 %
,必须用 %
.
在编写输出空行的批处理文件时,还应考虑 DosTips 论坛主题 ECHO. FAILS to give text or blank line - Instead use ECHO/ 上发布的建议。
批处理文件的主要改进是更改 IF 条件,从而完全避免使用命令块。
@echo off
setlocal EnableExtensions DisableDelayedExpansion
%SystemRoot%\System32\mode.com con cols=110 lines=32
set npctier=0
set "npctype=Wooden Dummy"
:randomnpc
if %npctier% NEQ 0 goto CombatChoice
set /A npclvl=%random% %% 5 + 1
set /A npchp=npclvl*100
set npcdmg=0
set /A npcdef=npchp*5/100
set /A npcxp=npclvl*100
:CombatChoice
echo/
echo You see %npctype% level %npclvl%.
echo/
echo The %npctype%'s health: %npchp% HP
echo/
endlocal
另见 Why is no string output with 'echo %var%' after using 'set var = text' on command line? 它包含一些关于如何使用命令 SET 的提示,但未写入此命令的 help/documentation。
如果你想保持相同的结构,那么像这样的东西会更好:
@Echo Off
SetLocal EnableExtensions
%__APPDIR__%mode.com 110, 32
Set "npctier=0"
:randomnpc
If %npctier% Equ 0 (
Set "npctype=Wooden Dummy"
Set "npcdmg=0"
Set /A "npclvl=(%RANDOM% %% 5) + 1"
Set /A "npchp=npclvl * 100, npcdef=npclvl * 5, npcxp=npchp"
)
:combatchoice
Echo=
Echo You see %npctype% level %npclvl%.
Echo=
Echo The %npctype%'s Health: %npchp% HP
Echo=
GoTo main
您应该注意,可以在一个算术指令中使用 Set /A
定义多个值
在您因创建重复代码的冗长脚本而太得意忘形之前,我认为值得向您介绍批处理宏。
Batch 中的宏是分配给变量的命令或命令块,通过使用 For 循环结合 If / Else 条件,可以使用捕获参数允许变量用作函数。
下面的示例包含宏的两个变体,可用于轻松生成不同的遭遇类型,只需最少的重复脚本,同时还可避免使用低效的函数调用。
宏必须在启用延迟扩展之前定义,因为在宏执行期间引用的变量是使用 !
扩展定义的,以便宏在扩展时解析的值是当时变量的值解析,而不是定义期间的值。
@echo off
mode con cols=110 lines=32
(Set \n=^^^
%= macro newline DNR =%
)
(Set LF=^
%= Linefeed DNR =%)
rem Example 1: npc generation; Fixed Formula macro
rem USAGE: %npc{generic}%{Npc name / type}{npc max level}{npc multiplier}{damage value or formula}
Set npc{generic}=For %%n in (1 2)Do if %%n==2 (%\n%
For /F "Tokens=1,2,3,4 Delims={}" %%G in ("!Params!")Do (%\n%
Set "npctype=%%G" %\n%
Set /a "npclvl= !random! %% %%H + 1" %\n%
Set /a "npchp= !npclvl! * %%I" %\n%
Set /a "npcdmg= %%J" %\n%
Set /a "npcdef= ( !npchp! * %%H ) / %%I" %\n%
Set /a "npcxp=!npclvl! * %%I" %\n%
echo/Enemy: !npctype!!LF!level: !npclvl!!LF!HP: !npchp!!LF!Damage: !npcdmg!!LF!Defence: !npcdef!!LF!XP: !npcxp!!LF!%\n%
)%\n%
) Else Set Params=
rem Example 2: npc generation; Supplied Formula macro
rem USAGE: %npc{boss}%{Npc name / type}{level value or formula}{hp value or formula}{damage value or formula}{defense value or formula}{xp value or formula}
Set npc{boss}=For %%n in (1 2)Do if %%n==2 (%\n%
For /F "Tokens=1,2,3,4,5,6 Delims={}" %%G in ("!Params!")Do (%\n%
Set "npctype=%%G" %\n%
Set /a "npclvl= %%H " %\n%
Set /a "npchp= %%I " %\n%
Set /a "npcdmg= %%J " %\n%
Set /a "npcdef= %%K " %\n%
Set /a "npcxp= %%L " %\n%
echo/Enemy: !npctype!!LF!level: !npclvl!!LF!HP: !npchp!!LF!Damage: !npcdmg!!LF!Defence: !npcdef!!LF!XP: !npcxp!!LF!%\n%
)%\n%
) Else Set Params=
rem enable delayed expansion after macro definitions
setlocal enableextensions enabledelayedexpansion
:randomnpc
%npc{generic}%{Wooden Dummy}{5}{100}{0}
Pause
%npc{boss}%{Dragon}{!random! %% 10 + 10}{npclvl * 200}{npchp / 20}{npchp / (npclvl / 2)}{npclvl * 150}
Endlocal
Goto :Eof