read from hh:mm from text file and add upon

December 30, 2010 at 21:57:29
Specs: Windows 7
Alright, what I would like to do is read 4 lines from a text file line 2 and 4 contains the actual numbers in hh:mm, while 1 and 3 are just labels.
I want to set each line as its on variable and then ask the user input an amount to be added to each, for a new total so far this is what I have

@echo off
setlocal enabledelayedexpansion
set nbr=1

for /f "tokens=*" %%1 in (filename.txt) do (
set var!nbr!=%%1
set /a nbr+=1
if !nbr! gtr 4 goto process
echo %var1% has a total of %var2%
echo %var3% has a total of %var4%

set /p var5=How much additional for %var1%?
set /p var6=How much additional for %var3%?

set /a var2=var2+var5
set /a var4=var4+var6

del filename.txt

echo %var1% >> filename.txt
echo %var2% >> filename.txt
echo %var3% >> filename.txt
echo %var4% >> filename.txt

The top half seems to run fine if i were to just tell it to echo the variables... the problem comes to when adding them. Is there a way to add hh:mm and over 24 hours.

So if the first amount was lets say 21:35 and the user wanted to add an additional 3 hours and 30 minutes, the final result would be 25:05. I don't even know where to start for that

I was going to try and do it in excel, haven't really ever used it and can't figure out a way of keeping a running total or whatever you may want to call it, so it just has one cell and you input the new amount to add to it in that same cell or only one adjacent cell. It seems to only want me to enter the addition to a new cell every time. and have the sum formula go out that many cells.

Thanks in advance.

See More: read from hh:mm from text file and add upon

Report •

December 31, 2010 at 01:23:33
First things first. If I understand your description, your first few lines look something like this:

:a label
:this doesn't matter
bla bla

You have a few hurdles.

[1] You need to deal with the colon to do an arithmetic SET.

[2] You need to be clear as to whether the hours before 10 have a leading zero.

[3] Once you get through that, you can work out the "carry". So that 70 MM becomes 1 HH 10 MM.

Life is too important to be taken seriously.


Report •

December 31, 2010 at 20:59:51
Thanks for the quick response, I think I have got it to work, with the few tests I have done I haven't received any errors, for the colon I just changed the form of the text document so that it would be as follows:


and then throughout the program if i wanted to see the colon i would display it.
as far as adding up the hours and min with a preceding 0 and carrying over the min and hours i did the following:

::variables from for /f
set hh1=%var2:~0,2%
set mm1=%var2:~2,2%
set hh2=%var4:~0,2%
set mm2=%var4:~2,2%

set /p var5=Additional hours for %var1%(hhmm):
set /p var6=Additional hours for %var3%(hhmm):

set hh3=%var5:~0,2%
set mm3=%var5:~2,2%
set hh4=%var6:~0,2%
set mm4=%var6:~2,2%

set /a Tha=hh1+hh3
set /a Tma=mm1+mm3
if !Tma! gtr 59 goto CARRYA

set /a Thb=hh2+hh4
set /a Tmb=mm2+mm4
if !Tmb! gtr 59 goto CARRYB

echo %var1% New Total: %Tha%:%Tma%
echo %var3% New Total: %Thb%:%Tmb%

del filename.txt

echo %var1% >> filename.txt
echo %Tha%%Tma% >> filename.txt
echo %var3% >> filename.txt
echo %Thb%%Tmb% >> filename.txt



set /a Tma=Tma-60
set /a Tha=Tha+1
if !Tma! lss 10 set Tma=0%Tma%
goto THMB

set /a Tmb=Tmb-60
set /a Thb=Thb+1
if !Tmb! lss 10 set Tmb=0%Tmb%
goto DISP

I didn't apply the preceding 0 to the hours because all the hours I am dealing with is above 10, but if needed I would assume just do it the same as the min.

If there is a cleaner and easier way of accomplishing this please post it.

Thanks again.

Report •

January 1, 2011 at 12:28:09
Thanks again Mechanix2Go, I was able to solve the math problem as i posted above and no problems yet, but since, I have been trying to come up with a way to check the input of the hours is exactly 4 characters, i did finde this code

if not "%fhr:~4%"=="" (
echo. ERROR: Incorrect format

which does great if what is entered is more than 4 characters, but i can't figure out a way to come back with an error if it is 3 or less.

i don't know if there is a simple edit to that code that could do it, but after giving up on find that, i tried do a loop type check,

set n=0
for %%a in (%fhr%) do (
set /a n+=1
if not %n%==4 (
echo. ERROR: Incorrect format
goto :HRS

but it just parses the whole input instead of each character of the variable so %n% is always 1

any suggestions?

Report •

Related Solutions

January 1, 2011 at 18:46:45
@echo off & setLocal EnableDELAYedeXpansion

set /p T= time to add ?:
if "!T:~4!" neq "" echo too long && goto :eof
if "!T:~3!" equ "" echo too short && goto :eof


Life is too important to be taken seriously.


Report •

January 1, 2011 at 19:27:23
Thanks again Mechanix, works great

Report •

Ask Question