如何从没有单位毫秒的命令 PING 获取响应时间?

How to get response time from command PING without unit ms?

我想知道是否有可能在末尾没有 "ms" 的情况下在变量中获取 ping 并能够与变量进行一些乘法运算。

我的实际代码:

@ECHO OFF
CALL :ping google.com
SET PING1=%ms%
ECHO %ping1%
GOTO :EOF

:ping
SET ms=Error
FOR /F "tokens=4 delims==" %%i IN ('ping.exe -n 1 %1 ^| FIND "ms"') DO SET ms=%%i
GOTO :EOF

要修复您现有的代码,您可以 set "ms=%ms:ms=%" 从值中删除 "ms"。

不久前我学了一个nifty method of pinging可能适合你的需要。

@echo off & setlocal
call :ping www.google.com ms

echo %ms%
goto :EOF   

:ping <host/IP> <return_var>
setlocal
set "responsetime=Error"
for /F %%I in (
    'wmic path win32_pingstatus where "address='%~1'" get responsetime /value'
) do 2>NUL set /a %%I
endlocal & set "%~2=%responsetime%" & goto :EOF

ms 添加到您的熟食中,FOR /F "tokens=4 delims==ms" ...