我需要在VLAN中ping通多个IP。
如何将2-4位数字变量解析为xx.xx?
If it is 4 digits(1111), it needs to be redefined as 11.11
If it is 3 digits(222), it needs to be 02.22
If it is 2 digits(77), it needs to be 00.77
我该怎么做?
我曾尝试对字符串进行计数,但是在iy时却失败了。
SET /P _inputname= Enter Your Input Number(2-4 Digits):
START cmd /k ping 10.%_inputname%.55 /t
START cmd /k ping 10.%_inputname%.66 /t
START cmd /k ping 10.%_inputname%.77 /t
答案 0 :(得分:3)
在数字前加上零, 并从第4个最后一个位置获得2个位置,并打印最后一个2个位置。
:: Q:\Test\2019\05\24\SO_56298026.cmd
@Echo off
for %%A in (1111,222,77) do call :sub %%A
SET /P _inputname= Enter Your Input Number(2-4 Digits):
Call :sub %_inputname%
goto :Eof
:sub
set var=0000%1
Echo %Var:~-4,2%.%Var:~-2%
示例输出:
> Q:\Test\2019\05\24\SO_56298026.cmd
11.11
02.22
00.77
Enter Your Input Number(2-4 Digits):33
00.33
答案 1 :(得分:0)
这是测试字符串长度的另一种方法,但是这也将不允许输入超过4个字符。
@echo off
setlocal EnableDelayedExpansion & set 4max=
:begin
set /p "str=Enter 2-4 digits: "
set "4max=%str:~4,1%
if defined 4max cls & echo you entered to many characters & goto begin
for /l %%i in (0,1,10) do if not "%str%"=="!str:~0,%%i!" (
set /a ln+=1
) else goto :done
:done
if %ln% equ 4 set str=%str:~0,2%.%str:~2,2%
if %ln% equ 3 set str=0%str:~0,1%.%str:~1,2%
if %ln% equ 2 set str=00.%str%
echo(%str%