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.
@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.
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.