Batch file to ping several hosts and log results
Since a connectivity issue between a Windows XP system in another country and
systems in the United States seemed to correlate with the time of day that
connectivity attempts were taking place, I wanted to have a batch file that
would periodically
ping from the source to the destination hosts and record
the results, so that I could determine if packet loss was occurring
at particular times every day because of contention with other traffic.
So I created the following batch file
(pinghosts.bat):
@echo off
set pingCount=5
set timeOut=500
set dirPath=%HOMEPATH%\Documents
REM ping google.com, apple.com, and cisco.com
for %%i in ("216.58.217.142" "17.142.160.59" "72.163.4.161") do (
if not exist %dirPath%\%%i.txt (
systeminfo | find "Time Zone:" > %dirPath%\%%i.txt
)
echo. >> %dirPath%\%%i.txt
echo %date% %time% >> %dirPath%\%%i.txt
ping -n %pingCount% -w %timeOut% %%i >> %dirPath%\%%i.txt
)
[ More Info ]
[/os/windows/commands/batch]
permanent link
Checking on process memory consumption on a Windows system
Sometimes when checking on excessive memory usage on a Microsoft Windows
system with the
Task Manager I see that many instances of a
particular process may be listed. I wanted a way from a command
prompt to total the amount of memory consumed by processes
with a particular name, e.g. chrome.exe, so I created a
showmemusage.bat batch file to allow me to specify a particular
process name and then have the script add the memory usage for all
processes with that name.
[ More Info ]
[/os/windows/commands/batch]
permanent link
Setting a Variable to be the Output of a Command
In a Windows batch file, I needed to set a variable to be the output of a
command. You can use the
for
command for that purpose as
shown below. In the example below, I wanted to count the number of
hpboid.exe
processes running on a system.
REM Count the number of hpboid.exe processes running.
for /f "delims=" %%a in ('tasklist /fi "imagename eq hpboid.exe" ^| find /c /i "hpboid.exe"') do set numprocesses=%%a
echo Number of hpboid.exe processes running: %numprocesses% >> %log%
The tasklist /fi "imagename eq hpboid.exe"
command uses
the tasklist
command with the /fi
option to filter
the output of running processes to just those named hpboid.exe
.
I then want to "pipe" the output to the find
command. I use
the /i
option to have find
ignore the case of the
letters in hpboid.exe
, i.e., I want to count a process whether it
is named hpboid.exe
or HPBOID.exe
, etc. I use the
/c
option to tell find
to display only the count of
lines containing the string hpboid.exe
.
For Windows, and I presume DOS as well, you
need to put a caret, i.e., a ^
in front of the pipe character,
|
character to "escape" how it would otherwise be interpreted.
The variable numprocesses
is set to be the count of the
hpboid.exe
processes running, which I then send to a log file,
i.e., %log%
, which is a variable that was set earlier in the
batch file.
References:
-
Escaping a Character in a Windows Batch File
Date: July 12, 2010
MoonPoint Support
-
Escape Characters
Date: January 17, 2008
Batcheero
-
Escape character
Wikipedia, the free encyclopedia
[/os/windows/commands/batch]
permanent link
Escaping a Character in a Windows Batch File
I had the following in a Microsoft Windows batch file:
REM Count the number of hpboid.exe processes running.
for /f "delims=" %%a in ('tasklist /fi "imagename eq hpboid.exe" | find /c /i "hpboid.exe"') do set numprocesses=%%a
echo Number of hpboid.exe processes running: %numprocesses% >> %log%
When I ran the batch file, I would see | was unexpected at this
time.
. I then realized I needed to "escape" the meaning of the |
at that point in the code. For Windows, and I presume DOS as well, you
can put a caret, i.e., a ^
, in front of a character to "escape"
how it would otherwise be interpreted. So the following worked, instead:
for /f "delims=" %%a in ('tasklist /fi "imagename eq hpboid.exe" ^| find /c /i "hpboid.exe"') do set numprocesses=%%a
If you are using a variable, e.g. %myvariable%
in a
for loop in a batch file,
you need to use another percent sign, i.e., a %
before each of
the %
characters used for the variable name. I.e., you need to
use %%myvariable%%
. You can think of the additional %
"escaping" the meaning of the other one.
References:
-
Escape Characters
Date: January 17, 2008
Batcheero
-
Escape character
Wikipedia, the free encyclopedia
[/os/windows/commands/batch]
permanent link