当我希望我的批次获得输入时,我使用:
set /p myvar=Type here:
通过运行代码并输入类似这样的内容空格:
这是输入。
我能得到四个字分别指定为一些变量,例如
1=This
2=is
3=the
4=input.
这样我就可以根据需要使用这些变量(%1%、%2%、%3%、%4%)?
或者是否有其他输入命令?
答案1
数组定义的另一种宏方法:
@Echo off
rem /* Array macro definition occurs prior to delayed expansion being enabled */
Set "DefUserArr=Set "#{i}=0"& Set /P "String=Enter #:"& For %%G in (!String!)Do (Set /a "#{i}=!#{i}!+1" & Set "#[!#{i}!]=%%G")"
rem /* Enable delayed expansion in order to expand the macro */
Setlocal EnableDelayedExpansion
rem /* Supply the variable name to be used for the Array group using substring modification as the macro is expanded */
%DefUserArr:#=Value%
rem /* Access the Group elements using the reference variable */
Set Value[
rem /* Groups begin from a '1' index and group count is referenced via the Groupname{i} variable */
Set Value{i}
Endlocal
输入输出示例:
C:\Users\tcdou>inputA
Enter Value:one two&<> three^!^!
Value[1]=one
Value[2]=two&<>
Value[3]=three!!
Value{i}=3
当然,如果您的脚本是为您自己以外的用户编写的,您可能至少需要在使用之前测试是否输入了输入以及“数组”是否定义。下面是一个稍微更强大的版本,演示了一个?ArrDef
测试这个的宏:
@Echo off & Setlocal DISABLEdelayedexpansion
rem /* Array macro definition occurs prior to delayed expansion being enabled */
Set "DefUserArr=Set "String="&For %%n in (1 2)Do if %%n==2 ((If /I Not "!Switch!" == "-A" (For /F "Tokens=1,2 Delims==" %%v in ('2^> Nul Set #[')Do Set "%%~v=" & Set "#{i}=0")) & Set /P "String=Enter #:"& For %%G in (!String!)Do (Set /a "#{i}=!#{i}!+1" & Set "#[!#{i}!]=%%~G"))Else Set Switch="
Set "?ArrDef=For %%n in (1 2)Do if %%n==2 ( If "!#{i}!" == "" ( For %%G in (!Actions!)Do %%~G )Else If "!#{i}!" == "0" ( For %%G in (!Actions!)Do %%~G ))Else Set Actions="
rem /* Enable delayed expansion in order to expand the macro */
Setlocal EnableDelayedExpansion
rem /* Supply the variable name to be used for the Array group using substring modification as the macro is expanded */
:Input1
%DefUserArr:#=Value%
rem /* test if array defined */
%?ArrDef:#=Value% "Echo/Inital input required" "Goto :Input1"
rem /* Access the Group elements using the reference variable */
Set Value[
rem /* Groups begin from a '1' index and group count is referenced via the Groupname{i} variable */
Set Value{i}
rem /* append to the array using the -A switch */
%DefUserArr:#=Value%-A
Set Value[
Set Value{i}
rem /* Using the DefUserArr macro without the -A switch will remove any preexisting values for the supplied group name. */
:Input3
%DefUserArr:#=Value%
%?ArrDef:#=Value% "Echo/New input required" "Goto :Input3"
Set Value[
Set Value{i}
Endlocal & Endlocal
答案2
@echo off && title <nul
title .\%~nx0 && color 0a
cd /d "%~dp0" && mode 50,10
setlocal enabledelayedexpansion
>con: echo/ & set /p "_myvar=Enter your Input:"
set "_myvar_0=%_myvar: =" & set/a "_i+=1+0" & set "_myvar_!_i!=%"
<con: ^< nul rem./ && for /l %%l in (0 1 !_i!)do echo=!_myvar_%%~l!
endlocal & goto=%:EOF
您可以尝试使用数组,方法发布在 dostips.com/经过@Aacini
原始布局代码如下:
set "_myvar=%_myvar: ="&set /a i+=1&set "_myvar[!i!]=%"
我更喜欢更换[ ]
一个简单的_
另外,改变i+=
到_i+=1+0
,这里不需要预定义set
命令:set i=0
set "_myvar_0=%_myvar: =" & set/a "_i+=1+0" & set "_myvar_!_i!=%"
1.您可以使用空格定义的分隔符访问所有出现的情况。
set "%_myvar: =" ==> "%_myvar:space="
2.该方法使用柜台其中限制是存储在!_i!
。
set /a _i+=1+0
3.使用for /L
循环,你可以访问所有事件,从0
到限制!_i!
:
for /l %%l in (0 1 !_i!)do...
4.%%l
通过将循环中的变量与分配给变量的名称连接起来_myvar_
,您可以获得每次出现的值......
for /l %%l in (0 1 !_i!)do echo=!_myvar_%%~l!