2013-12-10 152 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

非常感謝你! –