I am wondering if I can make a moving clock without having to refresh. This is my current code:
:clks
cls
echo.
echo ======
echo %DATE%
echo %TIME%
echo ======
timeout -t 1 >nul
goto clks
Try this:
#echo off
setlocal EnableDelayedExpansion
for /L %%i in () do (
cls
echo/
echo ======
echo !DATE!
echo !TIME!
echo ======
timeout -t 1 > nul
)
This is an near real time clock, using the ping command to create an delay short enough to visualize the milliseconds.
#echo off
#mode con cols=25 lines=3
title Clock
color 0f
:loop
echo %date%
echo %time%
for /l %%I in (1,1,2) do ping -n 01 127.0.0.1 > nul
cls
goto loop
The first block of code is mostly just visual enhancements. The pure clock code is:
#echo off
:loop
echo %date%
echo %time%
for /l %%I in (1,1,2) do ping -n 01 127.0.0.1 > nul
cls
goto loop
Instabilities like flashing can be fixed by increasing the number of pings in the for loop (currently 2), just be aware that doing so will make the clock run less smoothly.
#echo off
:12
title %time%
goto 12
Related
Having a latency less than 600 but greater than 90 continues to go to :FAST CONNECTION. It should be going to :MODERATE CONNECTION.
#ECHO off
MODE CON:cols=38 lines=11
:LOOP
SET a=3000
FOR /f "tokens=7 delims== " %%G IN ('PING -4 -n 1 8.8.8.8^| FIND "TTL" ') DO SET a=%%G
CLS
IF %a% EQU 3000 (GOTO :NO CONNECTION) ELSE (GOTO :SPEED)
:SPEED
IF %a% GTR 600 (GOTO :SLOW CONNECTION)else (IF %a% LSS 90 (GOTO :FAST CONNECTION) else (GOTO :MODERATE CONNECTION))
TIMEOUT /T 2 > NUL
:NO CONNECTION
COLOR 4F
ECHO.
ECHO.
ECHO --- NO CONNECTION ---
ECHO.
ECHO CHECK YOUR NETWORK CONNECTION
TIMEOUT /T 2 > NUL
CLS
ECHO.
ECHO.
ECHO *** NO CONNECTION ***
ECHO.
ECHO CHECK YOUR NETWORK CONNECTION
TIMEOUT /T 2 > NUL
CLS
ECHO.
ECHO.
ECHO !!! NO CONNECTION !!!
ECHO.
ECHO CHECK YOUR NETWORK CONNECTION
TIMEOUT /T 2 > NUL
GOTO :END
:FAST CONNNECTION
COLOR 2F
ECHO.
ECHO YOU CURRENTLY HAVE A
ECHO FAST CONNECTION TO INTERNET: %a%
ECHO.
ECHO APPLICATIONS AND FILE TRANSFERS
ECHO WILL RUN AT A GREAT RATE
ECHO.
ECHO FAST 0 - 10
ECHO MODERATE 11 - 20
ECHO SLOW 600 - 3000
TIMEOUT /T 3 > NUL
GOTO :END
:MODERATE CONNECTION
COLOR 6F
ECHO.
ECHO YOU CURRENTLY HAVE A
ECHO MODERATE CONNECTION TO THE INTERNET : %a%
ECHO.
ECHO APPLICATIONS AND FILE TRANSFERS
ECHO WILL RUN AT A SO/SO RATE
ECHO.
ECHO FAST 0 - 10
ECHO MODERATE 11 - 20
ECHO SLOW 600 - 3000
TIMEOUT /T 3 > NUL
GOTO :END
:SLOW CONNECTION
COLOR 4F
ECHO.
ECHO YOU CURRENTLY HAVE A
ECHO SLOW CONNECTION TO INTERNET: %a%
ECHO.
ECHO ALTERNATE OR CONTINGENCY
ECHO NETWORK WILL RUN AT A SLOWED RATE
ECHO.
ECHO FAST 0 - 10
ECHO MODERATE 11 - 20
ECHO SLOW 600 - 3000
TIMEOUT /T 3 > NUL
GOTO :END
:END
GOTO :LOOP
FOR /f "tokens=7 delims== " %%G IN ('PING -4 -n 1 8.8.8.8^| FIND "TTL" ') DO SET a=%%G
On a US-EN machine a gets set to a number with the time unit ms appended e.g. 100ms.
This causes the following comparisons to work as string comparisons (lexicographically), not number comparisons, so for example:
C:\etc>if 100ms LSS 11 #echo ???
???
C:\etc>if 100ms LSS 10 #echo ???
C:\etc>
The quick-and-dirty solution in this case is to strip the ms suffix right after the for loop.
set "a=%a:~0,-2%"
Note: this may not - and will likely not - work on localized Windows other than US-EN.
For the purpose of this demonstration, I've set a timeout of 3 seconds at the end of the script, you can adjust this as you please.
Your main issue is that you are using labels with spaces, you cannot do that. Second issue is that, as already mentioned by #Mofi in a comment, depending on your keyboard settings (language) different items are assigned to %a%.
I however suggest that you do 2 pings and use the average result, instead of ttl.
As a side note, ping (using ICMP) is for diagnostics and will not always get priority.
#echo off
MODE CON:cols=38 lines=11
:test
set latency=3000
#echo off
for /f "tokens=2delims==, " %%i in ('ping -4 -n 2 8.8.8.8^| find /i "average" ') do set "latency=%%i"
set latency=%latency:ms=%
if %latency% gtr 600 if %latency% lss 3000 (
color 4F
echo(
echo Slow connection %latency%
echo(
)
if %latency% geq 3000 (
color 4F
echo(
echo Connection seems down %latency%
echo(
)
if %latency% lss 90 (
color 2F
echo(
echo Fast Connection %latency%
echo(
)
if %latency% gtr 90 if %latency% lss 600 (
color 6F
echo(
echo Moderate Connection %latency%
echo(
)
(timeout /t 3)>nul 2>&1 & cls & goto test
You will note, for obvious reasons I removed some of the noise you had, you can add all the echo( back if you want, but I do not see the purpose of it. I also removed all the goto labels, as it is not required.
Please begin by taking note of my comment, which tells you that this code will not provide a measurable indication of your internet connection's speed.
Now, without radically changing your script and layout, you could probably use wmic with Win32_PingStatus, instead on ping. This should ensure that your variable always has the response time in milliseconds, if one is returned within the allowed timespan:
Example, (Just change the Host Address on line 5 as needed):
#Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Mode CON:Cols=45 Lines=11
Set "HA=8.8.8.8"
:Loop
Color
Set "RT="
For /F EOL^=R %%G In ('%__AppDir__%wbem\WMIC.exe Path Win32_PingStatus Where^
"Address='%HA%'" Get ResponseTime 2^>NUL')Do For %%H In (%%G)Do Set "RT=%%H"
If Not Defined RT GoTo None
If %RT% Gtr 600 GoTo Slow
If %RT% Gtr 90 GoTo Moderate
:Fast
Color 2F
Echo=
Echo YOU CURRENTLY HAVE A
Echo FAST CONNECTION TO INTERNET: %RT%ms
Echo=
Echo APPLICATIONS AND FILE TRANSFERS
Echo WILL RUN AT A GREAT RATE
Echo=
Echo FAST : 0 - 10
Echo MODERATE : 11 - 20
Echo SLOW : 600 - 3000
Echo=
%__AppDir__%timeout.exe /T 5 /NoBreak >NUL
GoTo Loop
:Moderate
Color 6F
Echo=
Echo YOU CURRENTLY HAVE A
Echo MODERATE CONNECTION TO THE INTERNET: %RT%ms
Echo=
Echo APPLICATIONS AND FILE TRANSFERS
Echo WILL RUN AT A SO/SO RATE
Echo=
Echo FAST : 0 - 10
Echo MODERATE : 11 - 20
Echo SLOW : 600 - 3000
Echo=
%__AppDir__%timeout.exe /T 5 /NoBreak >NUL
GoTo Loop
:Slow
ClS
Color 4F
Echo=
Echo YOU CURRENTLY HAVE A
Echo SLOW CONNECTION TO INTERNET: %RT%ms
Echo=
Echo ALTERNATE OR CONTINGENCY NETWORK
Echo WILL RUN AT A GREAT RATE
Echo=
Echo FAST : 0 - 10
Echo MODERATE : 11 - 20
Echo SLOW : 600 - 3000
Echo=
%__AppDir__%timeout.exe /T 5 /NoBreak >NUL
GoTo Loop
:None
ClS
Color 4F
Echo=
Echo --- NO CONNECTION ---
Echo=
Echo CHECK YOUR NETWORK CONNECTION
Echo=
%__AppDir__%timeout.exe /T -1
GoTo Loop
Please be aware, that I do not know why you've asked the code to loop back to the ping, so if that's not your overall intention, for now you'll need to use CTRL+C to stop if from looping.
I have a little script to ping several IP addresses that are located in a text file called computers.txt:
The output currently looks like this:
I would like to add a description of where the IP addresses are like so:
What would be the best way to do this?
#echo off
set fnm=C:\Users\jelliott\Desktop\computers.txt
set lnm=C:\Users\jelliott\Desktop\results.txt
if exist %fnm% goto Label1
echo.
echo Cannot find %fnm%
echo.
Pause
goto :eof
:Label1
echo PingTest STARTED on %date% at %time% > %lnm%
echo ================================================= >> %lnm%
echo.
for /f %%i in (%fnm%) do call :Sub %%i
echo.
echo ================================================= >> %lnm%
echo PingTest ENDED on %date% at %time% >> %lnm%
echo ... now exiting
goto :eof
:Sub
echo Testing %1
set state=alive
ping -n 1 %1 | find "TTL="
if errorlevel 1 set state=dead
echo %1 is %state% >> %lnm%
When adding the titles to your computers.txt like this:
1.2.3.4 Clinic
12.13.14.15 Registration
78.79.251.123 Somwhere else with spaces
You can alter your script a bit:
The for loop gets altered to: for /f "tokens=1*" %%i in (%fnm%) do call :Sub %%i "%%j"
Tokens specifies what part of the line you want to have. The default delimiters are spaces and new lines. So with this the first token of one line gets saved to %%i and the rest of the line (*) get saved to %%j as j is after i in the alphabet. The %%j need to get enclosed in double quotes to have multiple words together as one parameter.
Now change your subfunction to use the second parameter as well:
echo Testing %1 %~2
REM Other stuff to ping...
echo %~2: %1 is %state% >> %lnm%
The ~ removes the surrounding quotes again.
Example output:
PingTest STARTED on 11.01.2017 at 7:48:52,69
=================================================
Clinic: 1.2.3.4 is alive
Registration: 12.13.14.15 is dead
Somwhere else with spaces: 78.79.251.123 is alive
=================================================
PingTest ENDED on 11.01.2017 at 7:49:04,70
My friend wants to run this batch 24 hours. So basically it pings and print time
#echo off
set/p host=host Address:
set logfile=Log_%host%.log
echo Target Host = %host% >%logfile%
for /f "tokens=*" %%A in ('ping %host% -n 1 ') do (echo %%A>>%logfile% && GOTO Ping)
:Ping
for /f "tokens=* skip=2" %%A in ('ping %host% -n 1 ') do (
echo %date% %time:~0,2%:%time:~3,2%:%time:~6,2% %%A>>%logfile%
echo %date% %time:~0,2%:%time:~3,2%:%time:~6,2% %%A
timeout 1 >NUL
GOTO Ping)
He told me that when the ping time out the program stops. This is not desired. He wants the program to keep running and log the time it's out.
How to modify it?
This logs response time greater than 100 ms. Adapt as you will.
:loop
wmic /append:"textfile.txt" path win32_pingstatus where "address='127.0.0.1' and responsetime > 100" get responsetime,timestamprecord
timeout /t 5
goto loop
It's a bad idea to put GoTo into for loops.
See wmic /?, wmic path win32_pingstatus get /? for help..
I've currently made this but there is a unexpected error in the for loop.
Please help.
#ECHO OFF
COLOR 1F
TITLE (Ash's Script) Server Ping Pong
rem ---------------------------------------------------------------------------------------------------------------
ECHO Started %time% %date% > X:\Scripts\ServerPing.txt
rem ---------------------------------------------------------------------------------------------------------------
FOR /f "tokens=*" %%i in (X:\Scripts\ComputerLists\ServerList.csv) DO (
PING %%i -n 1 -w 3
IF ERRORLEVEL 0 (
ECHO %%i ONLINE %time%>> X:\Scripts\ServerPing.txt
) ELSE (
ECHO %%i OFFLINE %time%>> X:\scripts\ServerPing.txt
)
)
rem ---------------------------------------------------------------------------------------------------------------
IF ERRORLEVEL 0 is always true.
Use this instead:
if not errorlevel 1
Also change the redirection on the echo to files:
>>X:\Scripts\ServerPing.txt ECHO %%i ONLINE %time%
This eliminates a problem when the last character is a number (of a stream)
Another issue is that %time% is evaluated when the loop starts - to get a dynamic time in the log then enable delayed expansion and use !time! instead of %time%
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%)