1

我需要在可能包含数字连字符的批处理文件中接收输入,即。1-5 表示 1、2、3、4 和 5 作为用户的输入。

我知道如何从用户那里获取单个字符输入,但是将输入分成 5 个(或更多)单独的条目让我感到困惑。

4

2 回答 2

1
@echo off

set /P "input=Enter a number or range: "

for /F "tokens=1,2 delims=-" %%a in ("%input%") do (
   set lower=%%a
   set upper=%%b
)
if not defined upper set upper=%lower%
for /L %%i in (%lower%,1,%upper%) do (
   echo Process number %%i
)
于 2013-09-16T19:32:09.120 回答
0

您可以使用for /fwithdelims来拆分字符串。您可以使用for /L在一个范围内循环。在命令行上键入help for以了解循环的类型。

@echo off
set /p "input=Enter a number or range: "

REM The user must enter either a plain number or a range.  Either way, we split
REM the user input on the minus sign.  If there's no minus sign, then only
REM %upper% won't get a value.
for /f "usebackq delims=- tokens=1,2" %%a in ('%input%') do (
    set "lower=%%a"
    set "upper=%%b"
)

REM If %upper% has a value, then input was a range.  Otherwise, input contained
REM a single number.
if not "%upper%"=="" goto :handle_range

echo Single number: %lower%
goto :eof

:handle_range
echo Range %lower% to %upper%

REM We can use the numeric for loop to loop over the full range.
for /l %%i in (%lower%, 1, %upper%) do (
    echo     %%i
)

goto :eof
于 2013-09-16T18:01:39.417 回答