Timestamp in Batch File not Updating Properly -
i'll start off saying i'm new scripting...
i'm trying create batch file periodically pings host. @ moment, i'm testing on local pc. here's i've got far:
@echo off set servername=127.0.0.1 set limit=3 echo %date%, %time% starting ping test localhost>>c:\users\%username%\desktop\pingtest.txt /l %%x in (1,1,%limit%) ( ping %servername% -n 3 | find "reply" >>c:\users\%username%\desktop\pingtest.txt echo %time% >>c:\users\%username%\desktop\pingtest.txt timeout /t 5 ) exit
however, timestamp stays same. should show time being ~5 seconds after (or long timeout value set), stays same first timestamp. here's example of output:
25/08/2015, 2:09:18.34 starting ping test localhost reply 127.0.0.1: bytes=32 time<1ms ttl=128 reply 127.0.0.1: bytes=32 time<1ms ttl=128 reply 127.0.0.1: bytes=32 time<1ms ttl=128 2:09:18.34 reply 127.0.0.1: bytes=32 time<1ms ttl=128 reply 127.0.0.1: bytes=32 time<1ms ttl=128 reply 127.0.0.1: bytes=32 time<1ms ttl=128 2:09:18.34 reply 127.0.0.1: bytes=32 time<1ms ttl=128 reply 127.0.0.1: bytes=32 time<1ms ttl=128 reply 127.0.0.1: bytes=32 time<1ms ttl=128 2:09:18.34
is there way update proper time?
as side note, "for /l %%x in..." can't figure out should put in place of %%x. googling etc, i've seen people using different ones, can't seem figure out refers to. if let me know well, it'd appreciated.
thanks
at 1 point or another, literally every person scripts in batch fall delayed expansion trap.
basically, when batch script first run, variables in %variable%
format replaced actual values. when have code inside of code block (i.e. between (
, )
), variables may need update, can't, since presence of variable has been replaced value. around this, can put setlocal enabledelayedexpansion
@ top of script , use !variable!
format - tells script these need stay changeable.
@echo off setlocal enabledelayedexpansion set servername=127.0.0.1 set limit=3 /l %%x in (1,1,%limit%) ( ping %servername% -n 3 | find "reply" >>c:\users\%username%\desktop\pingtest.txt echo !time! >>c:\users\%username%\desktop\pingtest.txt timeout /t 5 )
and side note, %%x
variable chosen used in loop. can 1 letter long, , it's time batch variable case-sensitive. in case, can (%%x
fine) since you're not using directly , you're using run code 3 times.
Comments
Post a Comment