jakeythehobo
jakeythehobo

Reputation: 49

Timestamp in Batch File not Updating Properly

I'll start off by saying that I'm very new to scripting...

I'm trying to create a batch file that periodically pings a host. At the moment, I'm just testing it on my local PC. Here's what I've got so far:

@echo off

set SERVERNAME=127.0.0.1
set limit=3

ECHO %date%, %time% Starting ping test to localhost>>c:\users\%username%\desktop\Pingtest.txt

for /l %%X in (1,1,%limit%) do (

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, the timestamp always stays the same. It should show the time as being ~5 seconds after (or however long the timeout value is set), but stays the same as the first timestamp. Here's an example of the output:

25/08/2015,  2:09:18.34 Starting ping test to localhost
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
 2:09:18.34 
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
 2:09:18.34 
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
 2:09:18.34 

Is there any way to get it to update with the proper time?

As a side note, the "for /l %%X in..." I can't figure out what I should actually put in place of %%X. From Googling etc, I've seen people using different ones, but can't seem to figure out what it refers to. If someone could let me know about that as well, it'd be much appreciated.

Thanks

Upvotes: 2

Views: 3919

Answers (1)

SomethingDark
SomethingDark

Reputation: 14305

At one point or another, literally every person who scripts in batch will fall into the delayed expansion trap.

Basically, when a batch script is first run, variables in the %variable% format are replaced with their actual values. When you have code inside of a code block (i.e. between ( and )), the variables may need to update, but can't, since the presence of a variable has been replaced by its value. To get around this, you can put setlocal enabledelayedexpansion at the top of the script and then use the !variable! format - this tells the script that these need to stay changeable.

@echo off
setlocal enabledelayedexpansion

set SERVERNAME=127.0.0.1
set limit=3

for /l %%X in (1,1,%limit%) do (
    ping %servername% -n 3 | FIND "Reply" >>c:\users\%username%\desktop\Pingtest.txt
    echo !time! >>c:\users\%username%\desktop\Pingtest.txt
    Timeout /t 5
)

And for your side note, %%X is just the variable chosen to be used in the for loop. It can only be one letter long, and it's basically the only time that a batch variable is case-sensitive. In your case, it can be anything (%%X is perfectly fine) since you're not using it directly and you're just using it to run the code three times.

Upvotes: 8

Related Questions