【问题标题】:How to get response time from command PING without unit ms? [closed]如何从没有单位 ms 的命令 PING 获得响应时间? [关闭]
【发布时间】:2023-12-01 11:48:01
【问题描述】:

我想知道是否有可能在最后没有“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

【问题讨论】:

    标签: batch-file ping


    【解决方案1】:

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

    【讨论】:

    • 非常感谢老兄!
    【解决方案2】:

    要修复现有代码,您只需 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
    

    【讨论】:

    • 我不知道如何让你的代码工作编辑:Pastebin
    • @Hydroo 对不起,我修好了。
    最近更新 更多