【问题标题】:How to type in between two bracets-Batch File如何在两个大括号之间输入 - 批处理文件
【发布时间】:2013-03-24 01:08:47
【问题描述】:

嘿,我又来了,这次我想知道如何在输入变量的括号之间输入 IP 地址。

我希望代码看起来像这样。

set input=
set /p input=IP Address: [     ].[     ].[     ].[     ]

一旦我执行了批处理并输入了我的 IP,我希望它看起来像这样。

IP Address: [ 127 ].[ 0 ].[ 0 ].[ 1 ]

有没有办法做到这一点?

【问题讨论】:

  • 您需要第三方程序来实现这一点。

标签: variables batch-file windows-xp ip-address user-input


【解决方案1】:

我认为这将是一件非常复杂的事情。

如果您要尝试在批处理中执行此操作,则需要在用户每次敲击键盘时使用大括号重写您的行。到目前为止,我知道的唯一一个监听单个 KB-hit 的命令是 Choice 命令。 如果您以某种方式管理它以在每个 KB 命中时重复用户输入,您需要使用退格字符对批处理文件进行 bytehack,并具有将用户输入与大括号组合的逻辑。这不仅非常不舒服,而且非常缓慢。

我建议让您的用户输入任何字符串,然后通过“regex”检查它(windows regex 不是很好)

这是我用来检查打印机 IP 的示例函数。

%printer% 将是您的用户输入的字符串。这个函数还有一个 nslookup 函数。如果你愿意,你可以省略这个。 这第二行完成了所有的魔法,但你也可以输入一些不可能的 IP,比如 321.099.000.666

:CheckIP
echo %printer% | findstr /R "[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*">nul || goto :EOF
FOR /F "tokens=* delims=" %%A in ('nslookup %printer% 2^>^&1 ') do (
    echo %%A | findstr /R /C:"Name: ">nul && set printer=%%A
    echo %%A | findstr /R /C:"\*\*\*">nul && (set printerfqdn=%printer% & goto printerip)
)
rem ***************************************
rem * Name:    printername.location.companydomain
rem * printerfqdn begins at position 10
rem ***************************************
set printer=%printer:~9%
goto :EOF

【讨论】:

    猜你喜欢
    • 2014-09-06
    • 2012-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多