简体   繁体   English

使用Windows批处理脚本计时进程

[英]Timing a process with windows batch script

Hi and thanks in advance, 嗨,谢谢你,

I am newbie at developing batch files but I am trying to write a bat file right now that will create and write to a ldif file. 我是开发批处理文件的新手,但是我现在正在尝试编写一个bat文件,它将创建并写入ldif文件。 I am starting off with something simple 1st. 我从简单的第一开始。 I also need to time the process as it may get more complicated and I may need to do software comparisons later (such as bat vs. java later). 我还需要对过程进行计时,因为它可能变得更加复杂,并且以后可能需要进行软件比较(例如,稍后与bat对比Java)。

Right now I only got this: 现在我只有这个:

@ echo "LDIF Generator"
@ echo off
:: *** Enter the file here "within the double quotes"
set fileName="test.ldif"
:: *** If file exists, then erase all previous content
::if exist %fileName% ECHO Erase the file >%fileName%

echo Hello>%fileName%
echo Created LDIF File %fileName%


set /a sum = 0
for /L %%A in (1,1,5) do (
    echo %%A>>%fileName%  set a/ sum =  "%sum% + %%A"
)
echo sum is %sum%
pause

I am currently trying to figure out how to add up some numbers and time the process. 我目前正在尝试弄清楚如何将一些数字加起来并安排时间。

It gives me this output so far: 到目前为止,它给了我这个输出:

Hello
1  set a/ sum =  0 + 1
2  set a/ sum =  0 + 2
3  set a/ sum =  0 + 3
4  set a/ sum =  0 + 4
5  set a/ sum =  0 + 5

And on cmd sum is "0" 在cmd上的总和是“ 0”

I am trying to it to show the current iterate sum and echo back the finally sum at the end and the time it takes to do that. 我正在尝试显示当前的迭代总和,并在结束时回显最后的总和以及执行此操作所需的时间。

@ECHO OFF &SETLOCAL
set /a sum = 0
for /L %%A in (1,1,5) do (
    set /a sum+=%%A
)
echo sum is %sum%


sum is 15

Time calculations in batch are a major pain in the neck. 批量计算时间是脖子上的主要痛苦。 Check the answers to this question for different methods, ranging from a batch script to external tools. 检查该问题的答案是否有不同的方法,从批处理脚本到外部工具。 Personally I'd prefer the PowerShell Measure-Command cmdlet. 就个人而言,我更喜欢PowerShell Measure-Command cmdlet。

Use Endoro's code or something like it. 使用Endoro的代码或类似的代码。 That is simpler. 那比较简单。 But by way of explanation... to use code like yours you need to enabledelayedexpansion and use ! 但是作为解释……使用类似您的代码,您需要启用elayedexpansion并使用! instead of % within a FOR or IF construct to specify that you want to use the run-time value of the variable sum instead of the load-time value. 而不是FOR或IF构造中的%来指定您要使用变量sum的运行时值而不是加载时值。

setlocal enabledelayedexpansion
set /a sum = 0
for /L %%A in (1,1,5) do (
    echo %%A>>%fileName%  set a/ sum =  "!sum! + %%A"
)
echo sum is %sum%
endlocal

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM