Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Batch to execute in the start of windows  (Read 8502 times)

0 Members and 1 Guest are viewing this topic.

Esgrimidor

    Topic Starter


    Hopeful

    Thanked: 1
    Batch to execute in the start of windows
    « on: January 22, 2014, 07:51:53 PM »
    batch to execute in the start of windows

    I would like to save the time of the system to a variable of the system (that is a variable, environment variable or user variable, that is saved even when the system is shut off and can be recovered later)



    In the next restart recovered the variable predefined and compare with the present time.

    do a decision branch if the difference between the present time and the saved time is positive or negative lapsus of time.

    That is :

    Suppose you enter in the system at 16:40 hours

    Nex time you enter at :
    a) 18:50
    b) 11:30

    in the case a) the difference is 18:50 - 16: 40 = 02:10  two hours and ten minutes of difference
    in the case b) the difference is 11:30 - 16:40  = - 04:50 four hours and fifty minutes of negative difference.

    Really I only need to decide what time is prior or post the predefined time. Positive is the time is "later".

    Best Regards


    No dejes que tu felicidad dependa de lo que no depende de tí.

    foxidrive



      Specialist
    • Thanked: 268
    • Experience: Experienced
    • OS: Windows 8
    Re: Batch to execute in the start of windows
    « Reply #1 on: January 22, 2014, 11:23:19 PM »
    batch to execute in the start of windows

    I would like to save the time of the system to a variable of the system (that is a variable, environment variable or user variable, that is saved even when the system is shut off and can be recovered later)

    You can use a file which will preserve the time between reboots.  Does that suit you?

    Just to clarify, when you type echo %time% at the cmd prompt, what format does it return?  24 hour time or 12 hour time with AM/PM ?



    Squashman



      Specialist
    • Thanked: 134
    • Experience: Experienced
    • OS: Other
    Re: Batch to execute in the start of windows
    « Reply #2 on: January 23, 2014, 06:31:10 AM »
    But you would also have to take into consideration the day should you shutdown the computer on Wednesday and restart on Thursday.

    Esgrimidor

      Topic Starter


      Hopeful

      Thanked: 1
      Re: Batch to execute in the start of windows
      « Reply #3 on: January 23, 2014, 03:41:48 PM »
      I have several systems, so is no problem to have a peculiar system.

      Can anyone put the way to do what i need ?

      Best Regards
      No dejes que tu felicidad dependa de lo que no depende de tí.

      foxidrive



        Specialist
      • Thanked: 268
      • Experience: Experienced
      • OS: Windows 8
      Re: Batch to execute in the start of windows
      « Reply #4 on: January 23, 2014, 06:58:26 PM »
      Did I ask a question?

      Esgrimidor

        Topic Starter


        Hopeful

        Thanked: 1
        Re: Batch to execute in the start of windows
        « Reply #5 on: January 31, 2014, 03:25:09 PM »
        Ejem.

        Ready to put the question marks....
         ::)
        No dejes que tu felicidad dependa de lo que no depende de tí.

        patio

        • Moderator


        • Genius
        • Maud' Dib
        • Thanked: 1769
          • Yes
        • Experience: Beginner
        • OS: Windows 7
        Re: Batch to execute in the start of windows
        « Reply #6 on: January 31, 2014, 05:38:53 PM »
        Ejem.

        Ready to put the question marks....
         ::)

        They are above...in his Post.
        " Anyone who goes to a psychiatrist should have his head examined. "

        Salmon Trout

        • Guest
        Re: Batch to execute in the start of windows
        « Reply #7 on: February 01, 2014, 01:42:09 AM »
        Esgrimidor, could you post the result of this command so I can see your system time format

        echo %time%



        Esgrimidor

          Topic Starter


          Hopeful

          Thanked: 1
          Re: Batch to execute in the start of windows
          « Reply #8 on: February 01, 2014, 04:47:17 PM »
          Finally i understand. Sorry.

          23:55:18,55

           :-X
          No dejes que tu felicidad dependa de lo que no depende de tí.

          Salmon Trout

          • Guest
          Re: Batch to execute in the start of windows
          « Reply #9 on: February 01, 2014, 05:08:17 PM »
          Finally i understand. Sorry.

          23:55:18,55

           :-X

          OK watch this space

          First time of running you need to get time now as seconds since midnight and save it in a file and next boot time, read the file and do some simple arithmetic.

          I am going to bed now (it is 0:21:01.91) and will post more tomorrow


          foxidrive



            Specialist
          • Thanked: 268
          • Experience: Experienced
          • OS: Windows 8
          Re: Batch to execute in the start of windows
          « Reply #10 on: February 02, 2014, 02:04:21 AM »
          Salmon Trout, here's a snippet from Ritchie Laurence to get seconds from 1970



          Code: [Select]
          @echo off & setlocal ENABLEEXTENSIONS

          for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
          set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
          set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"


          call :DateToSecs %yy% %mm% %dd% %hh% %min% %sec% secs

          echo %secs%

          goto :EOF

          :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
          :DateToSecs %yy% %mm% %dd% %hh% %nn% %ss% secs
          ::
          :: By:   Ritchie Lawrence, updated 2002-08-13. Version 1.1
          ::
          :: Func: Returns number of seconds elapsed since 1st January 1970 00:00:00
          ::       UTC for a given calendar date and time of day. For NT4/2K/XP.
          ::
          :: Args: %1 year to convert, 2 or 4 digit (by val)
          ::       %2 month to convert, 1/01 to 12, leading zero ok (by val)
          ::       %3 day of month to convert, 1/01 to 31, leading zero ok (by val)
          ::       %4 hours to convert, 1/01 to 12 for 12hr times (minutes must be
          ::          suffixed by 'a' or 'p', 0/00 to 23 for 24hr clock (by val)
          ::       %5 mins to convert, 00-59 only, suffixed by a/p if 12hr (by val)
          ::       %6 secs to convert, 0-59 or 00-59 (by val)
          ::       %7 var to receive number of elapsed seconds (by ref)
          :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
          setlocal ENABLEEXTENSIONS
          set yy=%1&set mm=%2&set dd=%3&set hh=%4&set nn=%5&set ss=%6
          if 1%yy% LSS 200 if 1%yy% LSS 170 (set yy=20%yy%) else (set yy=19%yy%)
          set /a dd=100%dd%%%100,mm=100%mm%%%100
          set /a z=14-mm,z/=12,y=yy+4800-z,m=mm+12*z-3,j=153*m+2
          set /a j=j/5+dd+y*365+y/4-y/100+y/400-2472633
          if 1%hh% LSS 20 set hh=0%hh%
          if {%nn:~2,1%} EQU {p} if "%hh%" NEQ "12" set hh=1%hh%&set/a hh-=88
          if {%nn:~2,1%} EQU {a} if "%hh%" EQU "12" set hh=00
          if {%nn:~2,1%} GEQ {a} set nn=%nn:~0,2%
          set /a hh=100%hh%%%100,nn=100%nn%%%100,ss=100%ss%%%100
          set /a j=j*86400+hh*3600+nn*60+ss
          endlocal&set %7=%j%&goto :EOF
          :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

          :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
          :SecsToDate %secs% yy mm dd hh nn ss
          ::
          :: By:   Ritchie Lawrence, updated 2002-07-24. Version 1.1
          ::
          :: Func: Returns a calendar date and time of day from the number of
          ::       elapsed seconds since 1st January 1970 00:00:00 UTC. For NT4/2K/XP.
          ::
          :: Args: %1 seconds used to create calendar date and time of day (by val)
          ::       %2 var to receive year, 4 digits for all typical dates (by ref)
          ::       %3 var to receive month, 2 digits, 01 to 12 (by ref)
          ::       %4 var to receive day of month, 2 digits, 01 to 31 (by ref)
          ::       %5 var to receive hours, 2 digits, 00 to 23 (by ref)
          ::       %6 var to receive minutes, 2 digits, 00 to 59 (by ref)
          ::       %7 var to receive seconds, 2 digits, 00 to 59 (by ref)
          :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
          setlocal ENABLEEXTENSIONS
          set /a i=%1,ss=i%%60,i/=60,nn=i%%60,i/=60,hh=i%%24,dd=i/24,i/=24
          set /a a=i+2472632,b=4*a+3,b/=146097,c=-b*146097,c/=4,c+=a
          set /a d=4*c+3,d/=1461,e=-1461*d,e/=4,e+=c,m=5*e+2,m/=153,dd=153*m+2,dd/=5
          set /a dd=-dd+e+1,mm=-m/10,mm*=12,mm+=m+3,yy=b*100+d-4800+m/10
          (if %mm% LSS 10 set mm=0%mm%)&(if %dd% LSS 10 set dd=0%dd%)
          (if %hh% LSS 10 set hh=0%hh%)&(if %nn% LSS 10 set nn=0%nn%)
          if %ss% LSS 10 set ss=0%ss%
          endlocal&set %7=%ss%&set %6=%nn%&set %5=%hh%&^
          set %4=%dd%&set %3=%mm%&set %2=%yy%&goto :EOF
          :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

          Salmon Trout

          • Guest
          Re: Batch to execute in the start of windows
          « Reply #11 on: February 02, 2014, 02:49:05 AM »
          Salmon Trout, here's a snippet from Ritchie Laurence to get seconds from 1970

          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.



          Salmon Trout

          • Guest
          Re: Batch to execute in the start of windows
          « Reply #12 on: February 02, 2014, 03:02:14 AM »
          All that Esgrimidor seems to need, judging by the task description, is a magic number between 0 and 2359, which is simple to obtain.

          foxidrive



            Specialist
          • Thanked: 268
          • Experience: Experienced
          • OS: Windows 8
          Re: Batch to execute in the start of windows
          « Reply #13 on: February 02, 2014, 03:19:29 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.

          Oops, you are on the ball.  I forgot the original task as it's been so long.

          Quote from:
          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.

          I think Richie's datetime routines do cater for leap years etc.  but VBscript is preferable these days for simplicity and robustness, to a roll-your-own almost incomprehensible batch solution. :)


          Salmon Trout

          • Guest
          Re: Batch to execute in the start of windows
          « Reply #14 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?


          « Last Edit: February 02, 2014, 04:11:39 AM by Salmon Trout »