• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 569
  • Last Modified:

pipe a ping to a text file and have it make a new txt file every 1 MB.

I'm piping a ping to a text file on the desktop for monitoring purpose. I'd like to have it make a new text file every 'x' amount of size.

Any ideas how to do this easily in the same CMD window?
0
BryceRichert
Asked:
BryceRichert
  • 8
  • 8
  • 5
  • +1
1 Solution
 
dmeerenCommented:
Size of 1 MB is diffucult, why not split the files on the number of times a ping is done, see this code:
@echo off

Set lognr=1

:start
Set Count=1
:LOOP
Net Send * Hello
Set /A Count=%Count%+1
ping 10.30.0.100 -n 1 >>"PingLog-%lognr%-%DATE%.txt"
echo TIME:%time% >>"PingLog-%lognr%-%DATE%.txt"
If %Count% lss 1000 GoTo :LOOP

Set /A lognr=%lognr%+1
goto start

Open in new window

0
 
dmeerenCommented:
Sorry, wrong code, this is the correct one:
@echo off

Set lognr=1

:start
Set Count=1
:LOOP
Set /A Count=%Count%+1
ping 10.30.0.100 -n 1 >>"PingLog-%lognr%-%DATE%.txt"
echo TIME:%time% >>"PingLog-%lognr%-%DATE%.txt"
If %Count% lss 1000 GoTo :LOOP

Set /A lognr=%lognr%+1
goto start

Open in new window

0
 
BryceRichertAuthor Commented:
Do I just paste that into a CMD window and hit enter?
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
dmeerenCommented:
No, you make a new file ping.bat, then you right click it, and select edit with left mouse button. And then paste the code into that file.

Or you use the file i attached
0
 
dmeerenCommented:
Expert exchange does not support .bat files ;-(
0
 
Bill PrewCommented:
Here's an approach that will handle the flip when 1MB size exceded.  Take a look and let me know what questions you have. Save this as a BAT file and run.

@echo off

REM Define base name for logfiles, and max size allowed
set LogBase=%USERPROFILE%\Desktop\PingLog-
set MaxSize=1000000

REM Get a name for the first log file
call :GetNewLogName

REM Main loop to ping and log, and check size of log file
:PingLoop
  ping localhost >>"%LogName%"
  REM If log file too big, flip to a new one
  for %%A in ("%LogName%") do (
    if %%~zA GTR %MaxSize% call :GetNewLogName
  )
  goto :PingLoop

REM Safety measure to exit before subroutines just in case
exit /b

REM Subroutine to generate log file name with YYYYMMDD and HHMMSS in it
:GetNewLogName
  set YYYYMMDD=%DATE:~-4%%DATE:~-10,2%%DATE:~-7,2%
  set zTime=%TIME: =0%
  set zTime=%TIME::=%
  set HHMMSS=%zTime:~0,6%
  set LogName=%LogBase%%YYYYMMDD%-%HHMMSS%.txt
  exit /b

Open in new window

~bp
0
 
dmeerenCommented:
You Mean
%%~zA

Open in new window

reads the size of the file?
0
 
Bill PrewCommented:
Yes.

~bp
0
 
dmeerenCommented:
Thanks for that!
0
 
BryceRichertAuthor Commented:
when I run that bat it is making a bunch of files with nothing in them with this name that progresses by one:  PingLog-20110327-203218.txt
0
 
aavictorCommented:
a new text file every 'x' amount of size.

What is your meaning of every X amount of size?
example,  is it file size (or number of rows/ reply records)
                is it packet size of ping?
                is it to say 5KB of file?
                Or something you need to clarify.
However, what's your major purpose of ping test
Is it your PC? or client's PC? or Servers?
Why the size is more important than the connection issue? test us first
0
 
Bill PrewCommented:
==> when I run that bat it is making a bunch of files with nothing in them with this name that
==> progresses by one:  PingLog-20110327-203218.txt

Can you post the script that you are currently running, with any changes made to it.  I tested this here and it seemed to flip okay.

~bp
0
 
BryceRichertAuthor Commented:
I just don't want to have to open txt files that are 50 megs or larger. I just want them to be manageable.

The .bat I ran was wtih this: (no changes)

@echo off

REM Define base name for logfiles, and max size allowed
set LogBase=%USERPROFILE%\Desktop\PingLog-
set MaxSize=1000000

REM Get a name for the first log file
call :GetNewLogName

REM Main loop to ping and log, and check size of log file
:PingLoop
  ping localhost >>"%LogName%"
  REM If log file too big, flip to a new one
  for %%A in ("%LogName%") do (
    if %%~zA GTR %MaxSize% call :GetNewLogName
  )
  goto :PingLoop

REM Safety measure to exit before subroutines just in case
exit /b

REM Subroutine to generate log file name with YYYYMMDD and HHMMSS in it
:GetNewLogName
  set YYYYMMDD=%DATE:~-4%%DATE:~-10,2%%DATE:~-7,2%
  set zTime=%TIME: =0%
  set zTime=%TIME::=%
  set HHMMSS=%zTime:~0,6%
  set LogName=%LogBase%%YYYYMMDD%-%HHMMSS%.txt
  exit /b
0
 
dmeerenCommented:
Run mine! Run Mine! ;-)
0
 
Bill PrewCommented:
Hmm, I just retested this here and it worked exactly as expected.  The output of each PING is in the file, and at 1000000 bytes it started a new file.

Is there anything "interesting" about the system you are running this on?

And you are looking on the desktop for the output files, right?

~bp
0
 
dmeerenCommented:
No, Just some fun. "No fun, no glory"
0
 
BryceRichertAuthor Commented:
Windows 2008. I'll try it on a windows xp machine.
0
 
BryceRichertAuthor Commented:
I tried it on my xp desktop and it makes a log of 0 kb sized txt files in the same manner.

I just take that text, put it on my desktop as a txt file, rename the .txt ending to .bat and run it.
0
 
BryceRichertAuthor Commented:
What variables do I need to adjust in the .bat file? Where in that script do I set the destination ping?

thanks.
0
 
Bill PrewCommented:
When it runs, does it open a black box while it's running, and are there any messages displayed there?

You can adjust this line if needed, to specify the location and base name for the log file.

set LogBase=%USERPROFILE%\Desktop\PingLog-

You can adjust this line to customize the PING for your needs, I just had it hitting localhost.

ping localhost >>"%LogName%"

~bp
0
 
BryceRichertAuthor Commented:
so I can change ping localhost >>"%LogName%"

 to ping 192.168.x.x >>"%AnyFileName%"?
0
 
aavictorCommented:
Understand this command first
Simple example
ping 192.168.1.10 -n 11000 >  output.txt

You pings this IP in 1000 times within a period of time, and you put them in an output.txt in
current directory.  This command PING can do itself.  

Next,
For /L %i in (1,1,10) do @ping 192.168.1.10 -n 1000 > %i.txt
You pings this IP in 1000 times within a period of time, and you put them in an output.txt in
current directory.  This time you add a looping to repeat 10 time as an example. As a
result, you will 1 to 10 of text files.

Of course, you can make infiinite, but what do it mean to you?  For system administrator or
network admin, you maybe just want to know the connection of servers if they are on-line. Or
the speed of data communication.  However, there are some freewares in windows you can
check download.com.  They do this job without scripting and make your life easy.

IF you just want to get a survey of your network, you can change these counters: ping -n and looping
counter.  And Change Start number and End number for file name.  Otherwise, you may be
create a bomb to your hard disk and stress MS WIndow File System Indexing. This is a fact before
your start.

Anyway, have fun to try
0
 
BryceRichertAuthor Commented:
this one seems to be working now. thanks all!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

  • 8
  • 8
  • 5
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now