0

I'm trying to create a 1 line code where it adds up all variables its hard to explain in text right but here's a small part of the code

H = hours
M = Minutes
Thr = Total Hours
Tmin= Total Minutes

Call data.bat 
Rem data.bat is all the h1,m1,h2,m2,h3,m3... info

Rem Example Data
H1=3
M1=53
H2=4
M2=20
Set num=1
:refresh
if %H%num%% GTR 0 set /a Thr=%H%num%%+%Thr%
if %M%num%% GTR 0 set /a Thr=%M%num%%+%TMin%
if %M%num%% EQU 0 Goto :total
set /a number=%number%+1
goto :refresh
:Total
set /a Total=((%Thr%*60)+%Tmin%)
set /a TotalHour=(%Total%/60)
set /a TotalMin=(-%TotalHour%*60)+%Total%
if %Totalmin% GEQ 0 if %Totalmin% LEQ 9 set Totalmin=0%Totalmin%
Echo Duration    : %TotalHour%hr %TotalMin%min

Hours does not have if 0 goto total because there may be times where there was 0 hours but ##'s minutes Any Ideas on how to do this? Running it as is gives me

step by step operation

if %h%num%% gtr 0 set /a Thr=%h%num%%+%thr%
If %h1% gtr 0 set /a thr=%h1%+%thr%
If 3 gtr 0 set /a thr=**3+%thr%
if 3 gtr 0 set /a thr=3+1
3
  • if you trying to set 1 var with values of 2 other vars or show 2 vars, write it with the correct number of %. ie. if %H%%num% ... and set /a Thr=%H%%num%+%Thr% instead. each vars inside 2 %. Commented Sep 8, 2015 at 22:13
  • if Thr=2 H3=4 Num=3 then I want a way so I could change if %H%num%% GTR 0 set /a Thr=%H%num%%+%Thr% so it reads If 4 GTR 0 set /a THR=4+2 Commented Sep 9, 2015 at 2:21
  • @Paul: The OP is not trying to set 1 var with values of 2 other vars nor show 2 vars, but access one array element: set /a Thr=!H%num%!+%Thr% Commented Sep 9, 2015 at 12:38

3 Answers 3

2

There are several points in your code that needs to be adjusted:

.

if !H%num%! GTR 0 set /a Thr=!H%num%!+%Thr%
  • However, the set /a command can directly access array elements when just the name of the variable is given, so in this case it is not necesssary to expand the value:

.

set /a Thr=H%num%+Thr
set /a number=number+1

The last line may also be written this way:

set /a number+=1
  • If you want to check if a variable does not exist (and not confuse it with a variable with zero value), you may use if defined command:

.

if defined H%num% goto refresh
  • You used the wrong variable name a couple of times: number instead of num, and Thr in the minutes calculation.

.

This is the final code after completed previous modifications:

@echo off

REM H = hours
REM M = Minutes
REM Thr = Total Hours
REM Tmin= Total Minutes

REM Call data.bat 
Rem data.bat is all the h1,m1,h2,m2,h3,m3... info

Rem Example Data
set H1=3
set M1=53
set H2=4
set M2=20
Set num=1
:refresh
set /a Thr=H%num%+Thr
set /a Tmin=M%num%+Tmin
set /a num=num+1
if defined M%num% Goto :refresh
:Total
set /a Total=((Thr*60)+Tmin)
set /a TotalHour=(Total/60)
set /a TotalMin=(-TotalHour*60)+Total
if %Totalmin% LEQ 9 set Totalmin=0%Totalmin%
Echo Duration    : %TotalHour%hr %TotalMin%min

Output:

Duration    : 8hr 13min
Sign up to request clarification or add additional context in comments.

1 Comment

Don't have enough Rep right now but +1 I know it says not to but +2
2

you Need delayed Expansion:

setlocal enabledelayedexpansion
set h1=3
set x=1
echo !h%x%!

EDIT to Show how it works with the example in your comment:

setlocal enabledelayedexpansion
set Thr=2
set H3=4
set Num=3
    REM echo if !H%Num%! GTR 0 set /a Thr=!H%num%!+%Thr%
    echo if !H%Num%! GTR 0 set /a Thr+=!H%num%!
    REM if !H%Num%! GTR 0 set /a Thr=!H%num%!+%Thr%
if !H%Num%! GTR 0 set /a Thr+=!H%num%!
echo %Thr%

(note: the intended lines are redundant, just to display, what happens) I used a different syntax to add a value to a variable; Commented out your syntax. Both versions are working the same.

%h%num%% 

will be interpreted as (inserted underscores to Show, how the Parser groups it):

%h%_num_%% 

with %h%(probably) undefined (shown below as ~), %% interpreded as an escaped single literal %, resulting in:

~_num_% 

or without the placeholders:

num%

The same with delayed Expansion:

!H%Num%!

%num%being 3:

!H3!

which is getting expanded to:

4

User jeb wrote a great answer to How does the Windows Command Interpreter (CMD.EXE) parse scripts? (hard stuff, but helps to understand, what's going on)

1 Comment

I want to so if Thr=2 H3=4 Num=3 then I want a way so I could change if %H%num%% GTR 0 set /a Thr=%H%num%%+%Thr% so it reads If 4 GTR 0 set /a THR=4+2
0

If you are looking a formula to parse time and calculate duration, you might look this: Calculate Time difference in Windows batch file

2 Comments

its time, but the conversion from hh:mm:ss.ms is already done just need to find a way to make %h%num%% to = %h1% which equals a number
The question is not about Time difference, but precisely the opposite thing: add several times...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.