I need to have multiple remote admins test their download speeds at site. Speedtest.net and alike aren't a true indicator of performance in our WAN. I don't wish to use iperf as I need to minimise the technical knowledge and effort of the remote admins. I don't want them to have to install anything either. Therefore I need a simple batch file to download a test file 5 times. This is what I have so far:
@echo off
rem localize variables to this script
setlocal
rem delete the test file if it already exists
if exist %HomePath%\Downloads\100meg.test del %HomePath%\Downloads\100meg.test
rem perform the test 5 times
for /l %%i IN (1,1,5) DO (
rem mark the start time
set STARTTIME=%TIME%
echo Download started at:%STARTTIME%
rem start download
C:\windows\explorer.exe http://mirror.internode.on.net/pub/test/100meg.test
rem check for file download completion
:while
if exist %HomePath%\Downloads\100meg.test goto wend
goto while
:wend
rem mark the end time
set ENDTIME=%TIME%
echo Download completed at:%ENDTIME%
rem convert STARTTIME and ENDTIME to centiseconds
set /A STARTTIME=(1%STARTTIME:~0,2%-100)*360000 + (1%STARTTIME:~3,2%-100)*6000 + (1%STARTTIME:~6,2%-100)*100 + (1%STARTTIME:~9,2%-100)
set /A ENDTIME=(1%ENDTIME:~0,2%-100)*360000 + (1%ENDTIME:~3,2%-100)*6000 + (1%ENDTIME:~6,2%-100)*100 + (1%ENDTIME:~9,2%-100)
rem calculate the time taken in seconds
set /A DURATION=(%ENDTIME%-%STARTTIME%)/100
echo Download took:%DURATION% seconds
rem delete the test file ready for next iteration
del %HomePath%\Downloads\100meg.test
)
The problem is that since adding the for and enclosing the block in its brackets, it's stopped working. Can anyone shed any light on why this is failing?
Thanks!
Using GOTO within a FOR loop always breaks the loop - the remaining iterations will not occur once you use GOTO.
This is because of the way CMD.EXE works. The entire block of commands within the parentheses is parsed and loaded into memory all at once. Each iteration executes the already parsed commands that are stored in memory. These parsed commands do not include any label. GOTO must scan the file, which requires breaking the loop.
The "disappearing" value of STARTTIME also has to do with the fact that the entire block is parsed at once.
%STARTTIME%
is expanded at parse time, but the command is parsed before any of the block is executed. So you are getting the value that existed before the FOR command is executed, which is probably undefined. There is a feature called delayed expansion that can be used to get the value of a variable at execution time instead of parse time. TypeHELP SET
from the command prompt and read the section about delayed expansion that starts about half way through the documentation.But there is a simple way to get your code to work - just move the contents of the DO block to a subroutine and then CALL the routine within your loop. (I'm assuming your logic within the DO clause is otherwise correct). The CALL does not break the loop :-)
You must remember to put an EXIT /B before your routine label so that your main code does not fall through to the subroutine.