Read lines of text in txt files?

June 13, 2011 at 20:56:10
Specs: Windows 7
Ok I have a batch file that needs to...
1. Test if textfile.txt exists, if so then do nothing, if not then do this code:
echo.0>>"textfile.txt"
echo.0>>"textfile.txt"
echo.0>>"textfile.txt"
2. Read textfile.txt and set variables as following:
totdays = first line
tothours = second line
totmins = third line
3. Then I have the rest of the code, heres the end of it:
set /a totldays=%totdays% + %days%
set /a totlhours=%tothours% + %hours%
set /a totlmins=%totmins% + %mins%
DEL textfile.txt
echo.%totldays%>>"textfile.txt"
echo.%totlhours%>>"textfile.txt"
echo.%totlmins%>>"textfile.txt"
EXIT

And that's all it needs to do so please help.


See More: Read lines of text in txt files?

Report •


#1
June 14, 2011 at 15:31:54
I figured this out! But I have a new problem.... I have this code in the file I'm working on and it's not working properly:

if not %enddate% == %begindate% do (
if %enddate% GTR %begindate% do (
set /a days=%enddate% - %begindate%
)
if %enddate% LSS %begindate% do (
goto error
)
)
set /a ed=%enddate%
set /a bd=%begindate%
set /a et=%endtime%
set /a bt=%begintime%
if %et% == %bt% do (
goto error
)
if not %et% == %bt% do (
if %et% GTR %bt% do (
set /a tim=%et% - %bt%
set mins=%tim:~3,2%
set hours=%tim:~0,2%
if %et% LSS %bt% do (
goto error
)
)
set /a totldays=%totdays% + %days%
set /a totlhours=%tothours% + %hours%
set /a totlmins=%totmins% + %mins%
DEL textfile.txt
echo.%totldays%>>"textfile.txt"
echo.%totlhours%>>"textfile.txt"
echo.%totlmins%>>"textfile.txt"
EXIT

So when this file starts it shows you how long it's recorded then lets you record more time then adds it to the total time (the time is stored in textfile.txt temporarily):

@echo off
title=
SetLocal EnableDelayedExpansion
:if
if exist "textfile.txt" goto start
echo.0>>"textfile.txt"
echo.0>>"textfile.txt"
echo.00>>"textfile.txt"
:start
for /f "tokens=* delims= " %%a in (textfile.txt) do (
set /a N+=1
set v!N!=%%a
)
set totdays=!v1!
set tothours=!v2!
set totmins=!v3!
cls
echo Total time: %totdays% days %tothours%:%totmins%
echo Press any key to start. . .
pause >nul
goto begintim
:begintim
set begintime=%time:~0,2%%time:~3,2%
set begindate=%date:~-4%%date:~-10,2%%date:~-7,2%
goto hi
:hi
cls
echo Press any key when your done. . .
pause >nul
goto endtim
:endtim
set endtime=%time:~0,2%%time:~3,2%
set enddate=%date:~-4%%date:~-10,2%%date:~-7,2%
goto end
:end
if not %enddate% == %begindate% do (
if %enddate% GTR %begindate% do (
set /a days=%enddate% - %begindate%
)
if %enddate% LSS %begindate% do (
goto error
)
)
set /a ed=%enddate%
set /a bd=%begindate%
set /a et=%endtime%
set /a bt=%begintime%
if %et% == %bt% do (
goto error
)
if not %et% == %bt% do (
if %et% GTR %bt% do (
set /a tim=%et% - %bt%
set mins=%tim:~3,2%
set hours=%tim:~0,2%
if %et% LSS %bt% do (
goto error
)
)
set /a totldays=%totdays% + %days%
set /a totlhours=%tothours% + %hours%
set /a totlmins=%totmins% + %mins%
DEL textfile.txt
echo.%totldays%>>"textfile.txt"
echo.%totlhours%>>"textfile.txt"
echo.%totlmins%>>"textfile.txt"
EXIT
:error
cls
echo Sorry an error occured. . .
pause >nul
exit


Report •
Related Solutions


Ask Question