批处理-我想设置变量的第二部分
Batch-I want to set second part of variable
我有问题。
set /p command=
if %command% == "display (i want the second part of the variable here)" echo (second part of the variable)
例如,我输入:
display hello
我希望它简单:
echo hello
我想在我的游戏中将其用于自定义命令。
首先,您可以使用 "tokens=1*"
通过 for /f
循环拆分 firstword|notfirstword 上的文本。有关完整详细信息,请参阅控制台 window 中的 help for
。
接下来,您可以使用 attempt to call :label
,其中 :label
是第一个单词。本质上,您正在创建 batch functions and letting the user choose which function is executed. If the function label doesn't exist, then errorlevel will be non-zero and you can handle appropriately using conditional execution。这使得扩展您的脚本变得容易,而不必为您添加的每个选择或同义词添加 if /i
语句。 (通过重定向 2>NUL
来隐藏尝试 call
不存在的标签的错误消息可能是个好主意。)这是一个完整的示例:
@echo off & setlocal
:entry
set /P "command=Command? "
for /f "tokens=1*" %%I in ("%command%") do (
2>NUL call :%%I %%J || (
if errorlevel 1000 (exit /b 0) else call :unsupported %%I
)
goto :entry
)
:display
:echo
:say
:show
echo(%*
exit /b 0
:ping
ping %~1
exit /b 0
:exit
:quit
:bye
:die
echo OK, toodles.
exit /b 1000
:unsupported <command>
1>&2 echo %~1: unrecognized command
exit /b 0
我有问题。
set /p command=
if %command% == "display (i want the second part of the variable here)" echo (second part of the variable)
例如,我输入:
display hello
我希望它简单:
echo hello
我想在我的游戏中将其用于自定义命令。
首先,您可以使用 "tokens=1*"
通过 for /f
循环拆分 firstword|notfirstword 上的文本。有关完整详细信息,请参阅控制台 window 中的 help for
。
接下来,您可以使用 attempt to call :label
,其中 :label
是第一个单词。本质上,您正在创建 batch functions and letting the user choose which function is executed. If the function label doesn't exist, then errorlevel will be non-zero and you can handle appropriately using conditional execution。这使得扩展您的脚本变得容易,而不必为您添加的每个选择或同义词添加 if /i
语句。 (通过重定向 2>NUL
来隐藏尝试 call
不存在的标签的错误消息可能是个好主意。)这是一个完整的示例:
@echo off & setlocal
:entry
set /P "command=Command? "
for /f "tokens=1*" %%I in ("%command%") do (
2>NUL call :%%I %%J || (
if errorlevel 1000 (exit /b 0) else call :unsupported %%I
)
goto :entry
)
:display
:echo
:say
:show
echo(%*
exit /b 0
:ping
ping %~1
exit /b 0
:exit
:quit
:bye
:die
echo OK, toodles.
exit /b 1000
:unsupported <command>
1>&2 echo %~1: unrecognized command
exit /b 0