0

Recently I've been having problems with my internet connection. In order to find the problem in the chain I looked into some code I found online. I have never written code for a batch-file before, so just go ahead and expect me to know barrely anything. Anyway, I played around with it a bit and found out what each of the lines does.. to an extend at least. Now the results I'm getting from that file are included below as well. What I want to do is write a seperate file which I can run that takes the results from the first file and filters them into a new textfile.

I've already dug through some of the examples I've found here, but I don't simply want to copy paste (not that such a solution would work... I tried so I could reverse engineer it, but to no avail). I found some examples of how to get data from a file using a FOR and a FOREACH-Object, but it makes very little sense to me and I can't get it to work.

In short, I would like the pinging data that's placed in the first text file to be filtered by a batch file so it only sends part of the text to the new text file. Is anything like this possible and if so how do I go about this? Some explanation about how it works would be great as I like to take code apart and play around with it.

The code I use to get the pinging data whenever a connection fault is detected is listed below. I'm not saying it works perfectly mainly because I'm not 100% on how the %SystemRoot%-part works... But so far every time it triggers it also finds a request timeout. I'm open for suggestions to improve this but I would like for the focus to be on the noted challenge of extracting from one file and sending it to another.

:Loop
::Starting up the loop

PING -n 5 127.0.0.1 > NUL
::Set the ping delay

set Address=google.com
::Set the address to ping to google.com

%SystemRoot%\system32\ping.exe -n 1 %Address%|%SystemRoot%\system32\find.exe "TTL=" > NUL
::Check the connection

if %ERRORLEVEL% EQU 0 goto Loop
::If no error, return to Loop
::So if error, continue to next statement

echo Pinging %Address% >> D:\pingtest\Test\logfiletest.log
::Echo the pinging action

echo Trace route %Address% at %date% %time% >> 
D:\pingtest\Test\logfiletest.log
::Echo the route to trace and the timestamp

tracert %Address% >> D:\pingtest\Test\logfiletest.log
::Echo the pinging and results

echo. >> D:\pingtest\Test\logfiletest.log
::Nextline

goto Loop
::Return to Loop 

These are the results I'm getting that I'm trying to filter into the new file.

Pinging google.com 
Trace route google.com at Fri 05/24/2019 10:22:40.92 
Tracing route to google.com [172.217.168.238]

over a maximum of 30 hops:
1    <1 ms    <1 ms    <1 ms  MyLocation 
2    13 ms    12 ms    13 ms  195.190.228.150 
3     *        *        *     Request timed out.
4     7 ms    10 ms     8 ms  139.156.127.75 
5     7 ms     8 ms     9 ms  108.170.241.193 
6    15 ms    18 ms    17 ms  72.14.238.245 
7     8 ms     7 ms     7 ms  ams16s31-in-f14.1e100.net [172.217.19.206]

Trace complete.

So that is the result I am currently getting from the batch-file that I made after looking at the suggested code for another issue. Now what I would like to see something along the lines of the following

Trace route google.com at Fri 05/24/2019 10:22:40.92 
3     *        *        *     Request timed out.
5     7 ms     8 ms     9 ms  108.170.241.193 

I'm taking rows 3 and 5 as examples because they often return a 'Request timed out'

4
  • Your expected output is a little unclear. Do you always want lines 3 & 5? Do you want 2 different lines each time? A dynamic amount of lines? All lines? Only lines that time out? The complexity of this answer is based solely on the complexity of the output. Commented May 26, 2019 at 22:37
  • A timeout from tracert doesn't tell you anything useful. It only says that a particular server refuses to give an answer to a ping request. As you can see (the list continues) that doesn't break the trace. What exactly do you want to achieve? Commented May 27, 2019 at 10:37
  • @Stephan hmm that is a good point. I was hoping to find the point of origin for my connection problems, but as you point out this would probably not do that... Commented May 27, 2019 at 16:48
  • @BDM well the file that i want to extract from has a lot more than just this one result. For now i would like to extract the date-time indication along with the 2 rows from every message so that i can more easily compare the results from each result taking data from row 3 and 5. Commented May 27, 2019 at 16:50

2 Answers 2

1

The quick and dirty solution to this, assuming you already have a large logfile of items to go through, is to make a new batch file to filter the results.

When hardcoded, this is really simple however if you ever want dynamic results you'll need to heavily modify this.

for /f "tokens=1,*" %%i in (logfile.log) do (
    if "%%i"=="Trace" echo %%i %%j >> shortlog.log
    if "%%i"=="3" echo %%i %%j >> shortlog.log
    if "%%i"=="5" echo %%i %%j >> shortlog.log
)

This outputs

Trace route google.com at Fri 05/24/2019 10:22:40.92 
3     *        *        *     Request timed out.
5     7 ms     8 ms     9 ms  108.170.241.193 
Trace complete.

If you want to remove the last line, you can change your original code from
echo Trace route %Address% at %date% %time% >> D:\pingtest\Test\logfiletest.log
to
echo #Trace route %Address% at %date% %time% >> D:\pingtest\Test\logfiletest.log

and change my code to
if "%%i"=="#Trace" echo %%i %%j >> shortlog.log

Sign up to request clarification or add additional context in comments.

4 Comments

It may be dirty, but for now I haven't found a deviation from the lines that can contain connection errors. So as far as I'm concerned this works just fine. As for leaving out the last bit of text, I decided to keep it in. What I did do however is add if "%%i"=="Trace" (if "%%j"=="complete." (echo. >> D:\Pingtest\Test\shortlog.log)) to the code. This takes any row that contains 'Trace' followed by 'complete.' and skips a row after finding it. The result is a nice seperation between the many results by using a whitespace.
@AlwinG: ` if "%%i"=="Trace" (if "%%j"=="complete."` can be shortened to if "%%i% %j"=="Trace complete." for readabilty
@Stephan I think you misplaced a space there. Thanks to your comment I came up with if "%%i %%j"=="Trace complete." echo. >> D:\Pingtest\Test\shortlog.log Which works, but this replaces your %%i% %j by %%i %%j with the whitespace being in front of the double percentage. Is that wat you meant? Regardless thanks as this indeed did clarify and therefor improve the code
@AlwinG: yes, of course, it's %%i %%j. Sometimes a finger is faster than the others...
0

Pipe tracert to find.

Change this line

tracert %Address% >> D:\pingtest\Test\logfiletest.log

to

tracert %Address% | find "Request timed out." >> D:\pingtest\Test\logfiletest.log

find searches for lines containing Request timed out. and will output those lines.

View find /? for help.

If you want to understand the other commands, view command /? where command represents the command that you want help with.

Also view Using command redirection operators

1 Comment

Appreciate it, I'll look into it as soon as I get the chance

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.