循环的批处理脚本不起作用

循环的批处理脚本不起作用

本文介绍了循环的批处理脚本不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要让多个远程管理员在站点上测试其下载速度. Speedtest.net等并不是我们WAN中性能的真实指标.我不希望使用iperf,因为我需要尽量减少远程管理员的技术知识和工作量.我也不希望他们安装任何东西.因此,我需要一个简单的批处理文件来下载5次测试文件.这是我到目前为止的内容:

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
)

问题在于,由于添加了for并将该块括在其括号中,因此该块停止了工作.任何人都可以阐明失败的原因吗?

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?

谢谢!

推荐答案

在FOR循环中使用GOTO总是会中断循环-一旦使用GOTO,其余的迭代将不会发生.

Using GOTO within a FOR loop always breaks the loop - the remaining iterations will not occur once you use GOTO.

这是因为CMD.EXE的工作方式.括号内的整个命令块都被解析并一次加载到内存中.每次迭代都会执行已解析的命令,这些命令已存储在内存中.这些已解析的命令不包含任何标签. 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.

STARTTIME的消失"值还与立即解析整个块有关. %STARTTIME%在解析时被扩展,但是在执行任何块之前先解析命令.因此,您将获得执行FOR命令之前存在的值,该值可能是未定义的.有一个称为延迟扩展的功能,可用于在执行时而不是解析时获取变量的值.在命令提示符下键入HELP SET,并阅读有关延迟扩展的部分,该部分从文档的大约一半开始.

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. Type HELP SET from the command prompt and read the section about delayed expansion that starts about half way through the documentation.

但是有一种简单的方法可以使代码正常工作-只需将DO块的内容移至子例程,然后在循环内调用该例程即可. (我假设您在DO子句中的逻辑是正确的). CALL不会中断循环:-)

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 :-)

您必须记住在例程标签之前放置一个EXIT/B,以免您的主代码进入子例程.

You must remember to put an EXIT /B before your routine label so that your main code does not fall through to the subroutine.

@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 call :downloadTest
exit /b

:downloadTest
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
exit /b

这篇关于循环的批处理脚本不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 13:20