我需要在批处理文件中接收可能包含数字连字符的输入,即。 1-5表示来自用户的输入1,2,3,4和5。
我知道如何从用户那里获取单个字符输入,但是将输入分成5个(或更多个)单独的条目会让我感到困惑。
答案 0 :(得分: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
)
答案 1 :(得分:0)
您可以使用for /f
与delims
分割字符串。您可以使用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