如何正确地将FOR / F传递给批处理文件中的变量?

时间:2017-07-17 06:22:59

标签: batch-file cmd

我正在尝试创建一个执行ping的批处理文件,并将一些结果存储为变量。我正在尝试存储发送,接收,丢失和最小延迟的数据包,最大平均值。

我设法做到这一点,虽然使用单独的ping命令,因此每个变量的结果来自不同的数据集,并且彼此不相关:

            $text = '[X-PHP-Originating-Script: 0:acr.php

x: <193@example.com>

MIME-Version: 1.0

Content-Type: text/plain; charset=US-ASCII;

Format=flowed

Text-ID: <4854D382GA9@example.com>

Date: Sun, 02 Jul 2017 12:22:12 +0500]';

        preg_match('/Text\-ID:\s<(.*?)>/s', $text, $ret);
        var_dump($ret[1]);
        exit;

因此,为了尝试解决这个问题并让所有变量都从同一个ping命令中收集,我尝试将结果存储在.txt文件中,如下所示:

@ECHO OFF

FOR /F "tokens=4 delims=, " %%A IN ('ping -n 2 8.8.4.4 ^| findstr /R "^Packets: Sent =.$"') DO SET SENTPACKETS=%%A
ECHO Packets sent: %SENTPACKETS%

FOR /F "tokens=7 delims=, " %%A IN ('ping -n 2 8.8.4.4 ^| findstr /R "^Packets: Sent =.$"') DO SET RECEIVEDPACKETS=%%A
ECHO Packets received: %RECEIVEDPACKETS%

FOR /F "tokens=10 delims= " %%A IN ('ping -n 2 8.8.4.4 ^| findstr /R "^Packets: Sent =.$"') DO SET LOSTPACKETS=%%A
ECHO Packets lost: %LOSTPACKETS%

FOR /F "tokens=4 delims=,m " %%A IN ('ping -n 2 8.8.4.4') DO SET MINIMUMLATENCY=%%A
ECHO Minimum latency: %MINIMUMLATENCY%

FOR /F "tokens=9 delims=,m " %%A IN ('ping -n 2 8.8.4.4') DO SET MAXIMUMLATENCY=%%A
ECHO Maximum latency: %MAXIMUMLATENCY%

FOR /F "tokens=13 delims=,m " %%A IN ('ping -n 2 8.8.4.4') DO SET AVERAGELATENCY=%%A
ECHO Average latency: %AVERAGELATENCY%

pause

我知道我的min,max,avg延迟数字正确且可能数据包丢失(我不确定那个)但发送和接收的数据包数量是没有从正确的'令牌'读取,无论我尝试什么,我似乎无法从data.txt中的正确行读取它

有什么想法吗?

3 个答案:

答案 0 :(得分:1)

首先,使用

@echo off

>c:\data.txt ping 8.8.4.4

作为重定向器重定向该标准设备号之前的数字,而不是stdout

然后使用

单独替换前3个项目中的文件名
'findstr /R "^Packets: Sent =.$" c:\data.txt'

从文件中选择适当的行。您需要确保tokensdelims选项与您首次发布的例程中的选项匹配(第一个使用令牌4和delim ,,但您的第二个例程使用标记3并且仅仅作为空格区域化 - 也就是说,假设您的第一个代码令人满意地工作。

显然,前3个数据项的过滤器和选择具有不同的过滤器,因此需要将其应用于所有条目。

接下来,将2^>nul添加到要执行的命令'findstr /R "^Packets: Sent =.$" c:\data.txt 2^>nul'中以禁止讨厌的消息,并记住如果初始化先使用的变量,那么如果文件未能出现在文件中,则最初分配给变量的值是“未找到”消息。

哦,请尝试修改帖子,以包含典型的ping结果和结果报告,以及报告阅读的说明。

答案 1 :(得分:0)

@Echo off
:: clear variables
For %%A in (LatMin LatMax LatAvg PacSnt PacRcv PacLos) Do Set "%%A="
:: do the ping
For /f "tokens=1-8 delims=:=, " %%A in (
    'ping 8.8.4.4 2^>Nul ^| findstr "Pa Min"'
) Do If "%%A"=="Packets" (
    SET PacSnt=%%C
    SET PacRcv=%%E
    SET PacLos=%%G
) Else If "%%A"=="Minimum" (
    SET LatMin=%%B
    SET LatMax=%%D
    SET LatAvg=%%F
)
ECHO Packets sent    : %PacSnt%
ECHO Packets received: %PacRcv%
ECHO Packets lost    : %PacLos%

ECHO Minimum latency : %LatMin:~0,-2%
ECHO Maximum latency : %LatMax:~0,-2%
ECHO Average latency : %LatAvg:~0,-2%

答案 2 :(得分:0)

Magoo的代码效果很好!

@echo off

ping -n 100 8.8.4.4>c:\data.txt

for /f "tokens=4 delims=, " %%A IN ('findstr /R "^Packets: Sent =.$" c:\data.txt 2^>nul') do set sentpackets=%%A
echo Packets sent: %sentpackets%

for /f "tokens=7 delims=, " %%A IN ('findstr /R "^Packets: Sent =.$" c:\data.txt 2^>nul') do set receivedpackets=%%A
echo Packets received: %receivedpackets%

for /f "tokens=10 delims= " %%A IN ('findstr /R "^Packets: Sent =.$" c:\data.txt 2^>nul') do set lostpackets=%%A
echo Packets lost: %lostpackets%

for /f "tokens=4 delims=,m " %%A IN (c:\data.txt) do set minimumlatency=%%A
echo Minimum latency: %minimumlatency%

for /f "tokens=9 delims=,m " %%A IN (c:\data.txt) do set maximumlatency=%%A
echo Maximum latency: %maximumlatency%

for /f "tokens=13 delims=,m " %%A IN (c:\data.txt) do set averagelatency=%%A
echo Average latency: %averagelatency%

pause

不幸的是我无法让LotPings的代码工作,但我很感激回应。谢谢。