1.

Solve : Batch to execute in the start of windows?

Answer»

@echo off

set recordfilename="lastruntime.txt"
set firstrun=False
if not exist %recordfilename% set firstrun=True

REM Get current time
REM assumes 24 hr time between
REM 0:00:00.00 and 23:59:59.99
set nowtime=%time%

REM get 2 character HOUR string
set nhh=%nowtime:~0,2%

REM Remove leading space when using
REM using 24 hour time format (hour < 10)
if "%nhh:~0,1%"==" " set nhh=%nhh:~1,1%

REM If hour is "0" remove altogether
if "%nhh:~0,1%"=="0" set nhh=

REM get 2 digit minute string
set nmm=%nowtime:~3,2%

REM get 2 digit seconds string
set nss=%nowtime:~6,2%

REM get 2 digit seconds/100 string
set ncs=%nowtime:~9,2%

REM Create time string
set TimeString=%nhh%%nmm%%nss%%ncs%

REM Remove leading zero if hour was 0 and minute < 10
if "%TimeString:~0,1%"=="0" set TimeString=%TimeString:~1,255%

REM If not first run, get last run time from file
if "%firstrun%"=="False" set /p lastruntime=<%recordfilename%

REM Write this time record to file
echo %TimeString% > %recordfilename%

REM If this is first run, quit
if "%firstrun%"=="True" goto end

REM Subtract this time from last time
set /a diff=%TimeString%-%lastruntime%

if %diff% lss 0 goto Less
if %diff% equ 0 goto Equal
if %diff% gtr 0 goto More

:Less
Echo This time earlier than PREVIOUS
goto end

:Equal
Echo This time same as previous
goto end

:More
Echo This time LATER than previous

:end
Quote from: Salmon Trout on February 02, 2014, 02:49:05 AM

The OP wants to compare the time of day e.g. 11:30:35,22 with a stored value and decide if the difference is positive or negative.

A question to Esgrimidor: What to do if the two times are is exactly the same?

That script gives something like the Unix "epoch" figure*, in other WORDS. I haven't looked at that carefully yet, and I can't see if it knows about leap years, etc, but I will test it out later. I generally use either VBScript DateDiff or else GNU date for this sort of thing.

* The number of seconds that have elapsed since 00:00:00 Coordinated Universal Time (UTC), Thursday, 1 January 1970, not counting leap seconds.

Personally, I would need a very good reason not to use the VBScript Timer function for this. ("I don't understand VBScript" not counting as a good reason)

The system date string can be sliced in batch but there are leading zeroes and spaces etc to consider.

Hello again. The difference zero is a caso hard to consider, but we must to. Just because of an error or any other explanation.
Then.
1. If the difference is zero really nothing happen. Nothing will be done. The target is express a difference. So is a later decision to be taken.....

2. the most of the cases are when the difference is positive..... The time is passing by.

3. and when the difference is negative i will have to take a decision later.

So the important thing is express the difference in the algebraic sense : positive, negative, and even zero.
Just the calculation
Quote from: Salmon Trout on February 02, 2014, 03:49:30 AM
Esgrimidor,

1. You just show hours and minutes in your example, does that mean we can ignore seconds?

2. There are three possibilities:

a. Today's boot time is before the last boot time stored.
b. Today's boot time is the same as the last boot time stored.
c. Today's boot time is after the last boot time stored.

What to do if (b) applies?, i.e. if you want only two values, will they be "<=" and ">", or "<" and ">=" ?

3. Do you just want to know one of two or three things, "Less", "Equal", "More", etc, or do you need to know the time difference? If so, in what format?

Really I am trying to follow you. So don't follow me. I only need the difference is positive or negative, but to know the exact difference is much better.
I need to know the difference positive or negative to the second, because in a minute can change everything in this subject. To the second. Difference to the second. Not necessary to count fractions of seconds.....

About the possibilities is not RELEVANT to know when <= or >=, because the confusion of the moment where is =
so the three cases are :
<
>
=
Quote from: Salmon Trout on February 02, 2014, 08:18:58 AM
@echo off

set recordfilename="lastruntime.txt"
set firstrun=False
if not exist %recordfilename% set firstrun=True

REM Get current time
REM assumes 24 hr time between
REM 0:00:00.00 and 23:59:59.99
set nowtime=%time%

REM get 2 character hour string
set nhh=%nowtime:~0,2%

REM Remove leading space when using
REM using 24 hour time format (hour < 10)
if "%nhh:~0,1%"==" " set nhh=%nhh:~1,1%

REM If hour is "0" remove altogether
if "%nhh:~0,1%"=="0" set nhh=

REM get 2 digit minute string
set nmm=%nowtime:~3,2%

REM get 2 digit seconds string
set nss=%nowtime:~6,2%

REM get 2 digit seconds/100 string
set ncs=%nowtime:~9,2%

REM Create time string
set TimeString=%nhh%%nmm%%nss%%ncs%

REM Remove leading zero if hour was 0 and minute < 10
if "%TimeString:~0,1%"=="0" set TimeString=%TimeString:~1,255%

REM If not first run, get last run time from file
if "%firstrun%"=="False" set /p lastruntime=<%recordfilename%

REM Write this time record to file
echo %TimeString% > %recordfilename%

REM If this is first run, quit
if "%firstrun%"=="True" goto end

REM Subtract this time from last time
set /a diff=%TimeString%-%lastruntime%

if %diff% lss 0 goto Less
if %diff% equ 0 goto Equal
if %diff% gtr 0 goto More

:Less
Echo This time earlier than previous
goto end

:Equal
Echo This time same as previous
goto end

:More
Echo This time later than previous

:end

Running to try Salmon
i will comment
Thank you very much


Thanks a lot
Best Regards
Seems beautiful for me.
I have to put in the future in combination and will comment.
Simply perfect.


Discussion

No Comment Found