0

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

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"

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.

user2533789
  • 1,012
  • 2
  • 9
  • 14

3 Answers3

2
@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
Endoro
  • 34,892
  • 8
  • 45
  • 61
  • why is it when I put **for /L %%A in (1,1,5) do ( echo %%A>>%fileName% echo set /a sum+=%%A )** I get sum is 0? – user2533789 Jul 11 '13 at 00:45
  • You didn't say how you ran it. That code assumes you are running from a bat file. If you are executing from the command line all occurrences of %%A should be %A. Also, I suspect that the code you posted is not what you actually used (your echo statement would not produce the result posted). – RGuggisberg Jul 11 '13 at 07:09
  • @RGuggisberg I am running from a batch. I just click it and it runs. I am used to coding in Java and C#. So in that loop I expected to see all the sum iterations. Something like: **1 set a/ sum = 0 + 1 , 2 set a/ sum = 1 + 2 , 3 set a/ sum = 3 + 3 , 4 set a/ sum = 6 + 4 , etc.** – user2533789 Jul 12 '13 at 00:22
1

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.

Community
  • 1
  • 1
Ansgar Wiechers
  • 175,025
  • 22
  • 204
  • 278
0

Use Endoro's code or something like it. That is simpler. But by way of explanation... to use code like yours you need to enabledelayedexpansion and use ! 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.

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
RGuggisberg
  • 4,379
  • 1
  • 16
  • 23
  • Is this like using global variables vs. local variables? – user2533789 Jul 15 '13 at 21:42
  • No. The simple answer is... everything in a FOR loop, or IF statement, or within parenthesis is considered 1 line. The entire construct is loaded and variables expanded (regardless of how many physical lines the code spans). So varibles that you reference with %Variable% are expanded at LOAD TIME. With SETLOCAL ENABLEDELAYEDEXPANSION you specify that you have the option of expanding variables at RUN TIME by referencing them as !Variable!. Check put my post from a couple of years ago here. It may not be exactly correct, but give you a basic idea. http://ss64.org/viewtopic.php?id=985 – RGuggisberg Jul 16 '13 at 00:46