Is there anyway to have the output from this bat file into one long string e.g.
11/03/2015, 9:36:24.28, Packets: Sent = 1, Received = 0, Lost = 1 (100% loss), Minimum = 170ms, Maximum = 170ms, Average = 170ms
#ECHO off
set IPADDRESS=192.168.4.1
set INTERVAL=60
:PINGINTERVAL
echo %date% %time% >>CFConnection.txt
ping %IPADDRESS% -n 1 | FIND "loss" >>CFConnection.txt
ping %IPADDRESS% -n 1 | FIND "Minimum" >>CFConnection.txt
timeout %INTERVAL%
GOTO PINGINTERVAL
You could use set /P "=output"<NUL to write text to CFConnection.txt without a line break.
set /P "=output"<NUL
does pretty much the same thing as
echo output
but without a carriage return at the end.
You should also use findstr so you can match lines containing "loss" or "minimum" with one command, and so you're not trying to mash two ping results together into one dubious log entry of deceit and treachery. And finally, any time you want to capture the output of a command, use a for /f loop.
#echo off
setlocal
set "IP=192.168.4.1"
set "interval=60"
:loop
>>CFConnection.txt (
(
set /P "=%date% %time%, "<NUL
for /f "delims=" %%I in ('ping -n 1 %IP% ^| findstr /i "loss minimum"') do (
set /P "=%%I "<NUL
)
echo;
)
)
timeout %interval%
goto loop
Related
I'm trying to create a batch file where it would detect ping anomalies. I want it to ping to an IP infinitely (-t) until I close it where it would write down whenever ms > 100 ms and the time stamp as well. I'm thinking somewhere along the lines of sub string variables but I don't know how to wrap my head around it.
:Loop
time /t >> textfile.txt
ping -n 1 127.0.0.1 | findstr /c:"Minimum" >> textfile.txt
timeout /t 5
Goto Loop
Or perhaps this suits your needs
ping /t > textfile.txt
or
:loop
wmic /append:"textfile.txt" path win32_pingstatus where "address='127.0.0.1' and responsetime > 100" get responsetime,timestamprecord
goto loop
I've been looking for an answer to this same question for while. bgalea's answer gave me the pieces I needed to write my own. Here's what I came up with:
:: usage: badpings.bat [ip adress | hostname] [ping time threshhold]
#echo off
if "%1"=="" (
set pingdest=yahoo.com
) else (
set pingdest=%1
)
if "%2"=="" (
set /a limit=100
) else (
set /a limit=%2
)
echo Pinging %pingdest%.
echo Logging replies over %limit%ms.
echo Press Ctrl+C to end.
:Loop
for /f "usebackq tokens=1-6" %%a in (`ping -n 1 %pingdest% ^| findstr "Request Reply request"`) do (
set var=%%a %%b %%c %%d %%e %%f
set pingtimestr=%%e
)
if "%pingtimestr%"=="find" (
echo Ping request could not find host %pingdest%. Please check the name and try again.
goto End
)
if "%pingtimestr%"=="host" (
set /a pingtime=%limit%+1
)
if "%pingtimestr:~0,4%"=="time" (
set /a pingtime=%pingtimestr:~5,-2%
)
if %pingtime% GTR %limit% (
echo [%time%] %var%>>badpings.log
echo [%time%] %var%)
timeout /t 1 /nobreak >nul
Goto Loop
:End
It works on Windows 10. I haven't tested it on other OS versions.
I am trying to make a text-based game, and when the user enters something, I want the computer's character to have some kind of thinking animation.
I've come up with this: [. ] [.. ] [...]
Only problem is, I want it all to be on one line, so it's like an actual animation. I've successfully recreated this without removed the text already echo'd, but it requires that you cls, then send all that was on the screen before, three times. Obviously this takes too much space and is pretty inefficient.
I found a code that changes the last like without clearing all that was before it,
#echo off
echo This won't disappear!
setlocal EnableDelayedExpansion
for /f %%a in ('copy /Z "%~f0" nul') do set "CR=%%a"
for /L %%n in (5 -1 1) do (
<nul set /P "=This window will close in %%n seconds!CR!"
ping -n 2 localhost > nul
)
It counts down without erasing the echo before, but I do not understand how to adapt it to add periods to my string... Does anyone know a way to do this? Thanks!
You were almost there with the code you found; you just need to use your own text instead of somebody else's.
for /L %%A in (1,1,3) do (
set /p "=."<nul
ping -n 2 localhost >nul
)
If you want to put the periods inside of brackets, you can use this:
for /f %%A in ('"prompt $H &echo on &for %%B in (1) do rem"') do set BS=%%A
set /p "=[ ]"<nul
ping -n 2 localhost >nul
set /p "=%BS%%BS%%BS%%BS%. ]"<nul
ping -n 2 localhost >nul
set /p "=%BS%%BS%%BS%. ]"<nul
ping -n 2 localhost >nul
set /p "=%BS%%BS%.]"<nul
ping -n 2 localhost >nul
I'm doing a simple ping bandwidth test in batch to get speed in kb/s and I've run into a problem with the ms trailer after the average round trip time 123ms. The batch can't deal with the letters in the number token and it has no space to set another token with. I've looked at several solutions using delims= but did not success. I'm stuck and can't do anything except manually enter the number to get bandwidth. I'm running Windows XP and don't want to use anything but batch, i.e. no VBS, Java, hybred bat, etc. If it can't be done with simple command line I'll just manually enter the number and be done with it. Here is my batch:
#echo off
color 0b
MODE CON:COLS=57 LINES=15
for /F "tokens=9" %%a in ('ping -n 1 -l 1024 8.8.8.8^| find "Average"') do set "A1=%%a"
echo %A1%
set /a T=%A1%
set /a varia=1000/%T%
set /a answer=%varia%
set /a varia2=%answer%
set /a answer2=%varia2%
echo.
echo Speed %answer2% Kb/s
pause
and this from a answer here on stackoverflow
#echo off
color 0b
MODE CON:COLS=57 LINES=15
for /f "tokens=9 delims=()" %%a in (
'ping -n 1 -l 1024 8.8.8.8^| find "Average"'
) do (for /f "tokens=9" %%b in ("%%a") do (
set num=%%b & set num=!num:%%=!
if !num! == !num! goto nc
)
)
:nc
echo !num!
set /a T=!num!
set /a varia=1000/%T%
set /a answer=%varia%
set /a varia2=%answer%
set /a answer2=%varia2%
echo.
echo Speed %answer2% Kb/s
pause
Test this in XP: it works in Win 8.1
for /F "tokens=8 delims=ms=, " %%a in ('ping -n 1 -l 1024 8.8.8.8^| find "Average" ') do set "A1=%%a"
I'd like to be able to run a single batch file that would find the IP address of every device connected to my router, and then find their hostnames from it. What I have so far is below:
#echo off
set /a n=0
:repeat
set /a n+=1
echo 192.168.1.%n%
ping -n 1 -w 500 192.168.1.%n% | FIND /i "Reply">>devices.txt
if %n% lss 254 goto repeat
type devices.txt
goto :hostname
This finds all the possible addresses and writes them to a text file, devices.txt.
However, I now end up with the following:
Reply from 192.168.1.82: bytes=32 time=5ms TTL=255
The next part of the batch file goes:
:hostname
ping -a %ip% -l 1 -n 1 >> hostnames.txt
This pings the IPs found in devices.txt and returns the result in hostnames.txt - or at least, I'd like it to.
Somehow I need to separate the IP address from the other text when writing it to devices.txt, and then assign it as a variable so it can be used by the next part.
Is there a simple function I can use to do either of these things?
Try this:
#echo off
setlocal
set "p=ping -n 1 -w 500 192.168.1"
for /l %%a in (1,1,254) do (
for /f "Tokens=3 delims=: " %%b in (
'%p%.%%a^|Find /i "TTL="'
) do (
echo Pinging %%b
echo %%b>>devices.txt
for /f "tokens=2" %%c in (
'ping -a %%b -l 1 -n 1^|Find /i "pinging"') do (
echo %%c >>hostnames.txt
)
)
)
We start by creating a For /L loop that goes from 1 to 254 in increments of 1. Then we run a for loop on a ping command that takes the third token on the return line and puts it into a variable, %%b in this case. We then echo out pinging IP address contained in %%b and write it to devices.txt. Then, we run another ping to extract the second token which is the hostname of the computer we're pinging and write it out to hostnames.txt.
I'm creating a simple bat file that plays some ASCII animation stored in 120 .txt files. My script works but it works too fast. I'd like to find a way to slow it dow a bit. I've tried the TIMEOUT 1 command but it only plays 1 picture per second which is too slow. Is there a workable solution without moving from Windows 7 command line?
This is my script so far
#echo off
MODE CON: COLS=91 LINES=41
cls
:3
setlocal enableextensions enabledelayedexpansion
FOR /R %%i in (*.txt) do (type "%%i"
)
goto :3
You could use a FOR /L loop to introduce a delay. Here is a script that introduces an approximate 100 msec delay. A simple test near the top computes how many iterations are required to approximate 100 msec. The number will vary between machines. Adjust the definition of msecDelay as required to get your desired result.
#echo off
setlocal
:: Compute the number of iterations required to get the desired delay
set msecDelay=100
set ticks=100000
set "start=%time%"
for /l %%N in (1 1 %ticks%) do rem
set "stop=%time%"
for /f "tokens=3,4 delims=:.," %%A in ("%start%") do set /a start=1%%A%%B-10000
for /f "tokens=3,4 delims=:.," %%A in ("%stop%") do set /a stop=1%%A%%B-10000
if %start% gtr %stop% set /a stop+=6000
set /a delay=msecDelay*ticks/(stop-start)/10
MODE CON: COLS=91 LINES=41
cls
:3
FOR /R %%i in (*.txt) do (
type "%%i"
for /l %%n in (1 1 %delay%) do rem
)
goto :3
I'm wondering if you will get better results by moving CLS within the loop, just before your TYPE statement.
This is a very simple way to slow between frames
Example:
#echo exiting script
ping localhost -n 2 > nul
cls
#echo exiting script .
ping localhost -n 2 > nul
cls
#echo exiting script ..
ping localhost -n 2 > nul
cls
#echo exiting script ...
ping localhost -n 2 > nul
cls
#echo exiting script ....
ping localhost -n 2 > nul
cls
#echo exiting script .....
ping localhost -n 5 > nul
cls
Not built in, but still command line: sleep.exe from Windows Resource Kit (available here: http://www.microsoft.com/en-us/download/details.aspx?id=17657)
Usage: sleep time-to-sleep-in-seconds
sleep [-m] time-to-sleep-in-milliseconds
sleep [-c] commited-memory ratio (1%-100%)