我需要帮助从set / p中提取变量 我解释说:
set/p command=
:: THE %COMMAND% IS : setname mirtex
if "%command%"=="setname %name%" goto test
:test
echo your name is %name%
所以我想像echo mirtex一样提取我的名字“mirtex” 感谢
答案 0 :(得分:1)
您可以在第一个空格处拆分%命令%:
SET "name=%command:* =%"
ECHO Your name is %name%
如果%命令%确实包含双引号,请添加一个额外的步骤来删除结束语。
SET "name=%command:* =%"
SET "name=%name:~,-1%"
ECHO Your name is %name%
以下是基于我对您的回答的评论的简短示例:
:gfn
set/p "fname= Enter your first name "
if "%fname%"=="" goto :gfn
:gsn
set/p "sname= Enter your surname "
if "%sname%"=="" goto :gsn)
set "command=setname %fname% %sname%"
答案 1 :(得分:0)
要分割字符串,for
循环是最灵活的方式。
我不知道,如果你输入总会有两个元素。如果是的话:
for /f "tokens=1,2" %%a in (%command%) do (
set "command=%%a"
set "name=%%b"
)
echo command is "%command%", name is "%name%"
如果元素数量未知,最好使用通用变量名称:
@echo off
setlocal enabledelayedexpansion
set /p command=
set count=0
for %%a in (%command%) do (
set /a count+=1
set "element[!count!]=%%a"
)
echo number of elements: %count%
set element[