2013-12-10 154 views
0

我有一个使用psping和如下批处理脚本查找和替换

psping -l 8192 -i 1 -n 5 -w 0 localhost >> %outfile% 

然后输出输出到文件批处理脚本,我只是在寻找具有“回复”如下行:

findstr /N "Reply" %outfile% 

如你所知,所获得的线是按以下格式:

Reply from <IP>: 8.59ms 
Reply from <IP>: 9.18ms 
Reply from <IP>: 8.82ms 
Reply from <IP>: 9.40ms 
Reply from <IP>: 8.81ms 

然后我有这个subroutin e用逗号替换空格

findstr "Reply" %pingfile% >> %textfile% 
for /F "tokens=* delims= " %%a in (%textfile%) do @call :processeachline %%a 
endlocal 
goto :eof 
:processeachline 
setlocal 
set data=%* 
echo %data: =,% 
endlocal 
goto:eof 

以上结果在以下输出中显示。

Reply,from,<IP>:,8.81ms 

但我需要它在以下格式。

Reply from,<IP>,8.81,ms 

整个代码如下 关闭@echo @set本地 呼应日期%DATE%

@set tag=%DATE:~-4%-%DATE:~7,2%-%DATE:~4,2% 
set pingfile=psping%tag%.txt 

echo file name: %pingfile% 

if exist %pingfile% (
echo deleting existing ping file... 
del %pingfile% 
) 

set "tempfile=tempOut.txt" 
set "newfile=csvOutput%tag%.txt" 
if exist %tempfile% (
echo deleting existing temp output file... 
del %tempfile% 
) 


echo Ping started at %DATE% %TIME% >> %pingfile% 

REM Ping 5 times with an interval of 10 seconds between each with 0 warmup 
psping -i 1 -n 5 -w 0 cnn.com >> %pingfile% 



REM When done, parse the file and get only the necessary lines for the CSV 
findstr "Reply" %pingfile% >> %tempfile% 

REM parse the temp file and replace all spaces with commas and write to the csv 
for /F "tokens=* delims= " %%a in (%tempfile%) do @call :processeachline %%a 

goto :eof 
:processeachline 
set data=%* 
echo %data: =,% >> %newfile% 

for /F "tokens=*" %%a in ('findstr ms %newfile%') do @call :processeachlines "%%a" 

goto :eof 
:processeachlines 
set data=%~1 

echo %data:ms=,ms% 

@endlocal 

我如何去这样做(理想情况下,无需打开文本文件% %)?我必须使用标准的Windows工具,并且不能安装任何GNU软件包。 预先感谢您

+0

将所有的线是一样的吗?您可以在输出中使用':'作为分隔符,并使用硬编码“Reply from,localhost,'? (只有延迟会被'for'循环填充)。 – ixe013

回答

0
@echo off 
setlocal EnableDelayedExpansion 

findstr "Reply" %pingfile% >> %textfile% 
for /F "tokens=1*" %%a in (%textfile%) do (
    set rest=%%b 
    echo %%a !rest: =,! 
) 
+0

将代码更改为上面的代码,结果为:“Reply!rest:=,!” 。我错过了什么吗?此外,“本地主机”将根据我所ping的IP而有所不同。 –

+0

我想你错过了'setlocal EnableDelayedExpansion'行... – Aacini

0

这是从上面你的程序中,在分离的块中的一些变化:

findstr "Reply" %pingfile% >> %textfile% 
for /F "tokens=* delims= " %%a in (%textfile%) do @call :processeachline %%a 
endlocal 
goto :eof 
:processeachline 
setlocal 
set data=%* 

set data=%data: =,% 
set data=%data::=,% 
set data=%data:ms=,ms% 
set data=%data:Reply,from=Reply from% 

echo %data% 
endlocal 
goto:eof 
+0

非常感谢你! –