C:\_G\WWW\~ELISANET\INFO\tscmd147.html
<http://www.elisanet.fi/tsalmi/info/tscmd147.html>
Copyright © 2003- by Prof. Timo Salmi  
Last modified Tue 20-Nov-2018 14:48:29

 
Assorted NT/2000/XP/.. CMD.EXE Script Tricks
From the html version of the tscmd.zip 1cmdfaq.txt file
To the Description and the Index
 

This page is edited from the 1cmdfaq.txt faq-file contained in my tscmd.zip command line interface (CLI) collection. That zipped file has much additional material, including a number of detached .cmd script files. It is recommended that you also get the zipped version as a companion.

Please see "The Description and the Index page" for the conditions of usage and other such information.



147} What was the time 100 minutes ago?

  @echo off & setlocal enableextensions
  ::
  rem The 24h time format is assumed
  rem as always is sensible in an international setting
  ::
  :: Get the current time: hour and minutes

  for /f "tokens=1,2 delims=:" %%a in ('time /t') do (
    set hhNow_=%%a
    set mmNow_=%%b
    )
  :: Remove any leading zeros to avoid the octal trap
  for /f "tokens=* delims=0" %%a in ('echo %hhNow_%') do set hhNow_=%%a
  for /f "tokens=* delims=0" %%b in ('echo %mmNow_%') do set mmNow_=%%b
  ::
  :: Current time in minutes since midnight

  set /a mmSinceMidnight=60*%hhNow_%+%mmNow_%
  ::
  :: Display

  echo hhNow_ %hhNow_%
  echo mmNow_ %mmNow_%
  echo mmSinceMidnight %mmSinceMidnight%
  ::
  :: How many minutes to calculate backwards?

  set back_=0
  set rollover=
  set OutOfRange=
  if not "%~1"=="" set back_=%1
  if %back_% GTR 1440 set OutOfRange=true
  if %back_% LSS 0 set OutOfRange=true
  if defined OutOfRange (
    echo Parameter %back_% out of range
    goto :EOF
    )
  ::
  :: How many minutes since midnight was back_ minutes ago?

  set /a backSinceMidnight=%mmsinceMidnight%-%back_%
  if %backSinceMidnight% LSS 0 (
    set /a backSinceMidnight=%backSinceMidnight%+24*60
    set rollover=true
    )
  :: Display
  echo backSinceMidnight %backSinceMidnight%
  ::
  :: Convert the result to hours and minutes

  set /a hhBack_=%backSinceMidnight%/60
  set /a mmBack_=%backSinceMidnight%-60*%hhBack_%
  ::
  :: Display

  echo hhBack_ %hhBack_%
  echo mmBack_ %mmBack_%
  if defined rollover echo Yesterday
  ::
  endlocal & goto :EOF

The output e.g.
  C:\_D\TEST>cmdfaq 100
  hhNow_ 9
  mmNow_ 33
  mmSinceMidnight 573
  backSinceMidnight 473
  hhBack_ 7
  mmBack_ 53

The VBScript-aided solution is much more clearcut. And it spans midnights without any further measures:
  @echo off & setlocal enableextensions
  ::
  if "%~1"=="" (
    echo Usage: "%~0" MinutesToAdd
    goto :EOF)
  ::
  :: Build a Visual Basic Script and run it

  set vbs_=%temp%\tmp$$$.vbs
  set skip=
  ::
  findstr "'%skip%VBS1" "%~f0" > "%vbs_%"
  for /f "tokens=*" %%a in ('cscript //nologo "%vbs_%"') do (
    set DateTimeNow=%%a)
  ::
  findstr "'%skip%VBS2" "%~f0" > "%vbs_%"
  for /f "tokens=*" %%a in ('
    cscript //nologo "%vbs_%" "%~1" "%DateTimeNow%"') do (
      set DateTimeThen=%%a)
  ::
  :: Clean up

  for %%f in ("%vbs_%") do if exist %%f del %%f
  ::
  echo DateTimeNow =%DateTimeNow%
  echo DateTimeThen=%DateTimeThen%
  ::
  endlocal & goto :EOF
  '
  'The First Visual Basic Script

  FixedNow=Now 'VBS1
  Wscript.Echo FixedNow 'VBS1
  '
  'The Second Visual Basic Script

  Set arg = WScript.Arguments 'VBS2
  Wscript.Echo DateAdd("n", CLng(arg(0)), arg(1)) 'VBS2

The output could be e.g.
  C:\_D\TEST>cmdfaq -100
  DateTimeNow =17.04.2009 01:23:03
  DateTimeThen=16.04.2009 23:43:03

Should you wish to get the date and time elements into separate environment variables the solution is
  @echo off & setlocal enableextensions
  ::
  if "%~1"=="" (
    echo Usage: "%~0" MinutesToAdd
    goto :EOF)
  ::
  :: Build a Visual Basic Script and run it

  set vbs_=%temp%\tmp$$$.vbs
  set skip=
  ::
  findstr "'%skip%VBS1" "%~f0" > "%vbs_%"
  for /f "tokens=*" %%a in ('cscript //nologo "%vbs_%"') do (
    set DateTimeNow=%%a)
  ::
  findstr "'%skip%VBS2" "%~f0" > "%vbs_%"
  for /f "tokens=1-6" %%a in ('
    cscript //nologo "%vbs_%" "%~1" "%DateTimeNow%"') do (
      set yyyy_=%%a
      set mm_=%%b
      set dd_=%%c
      set hh_=%%d
      set nn_=%%e
      set ss_=%%f)
  ::
  :: Clean up

  for %%f in ("%vbs_%") do if exist %%f del %%f
  ::
  echo DateTimeNow =%DateTimeNow%
  echo yyyy_=%yyyy_%
  echo mm_=%mm_%
  echo dd_=%dd_%
  echo hh_=%hh_%
  echo nn_=%nn_%
  echo ss_=%ss_%
  ::
  endlocal & goto :EOF
  '
  'The First Visual Basic Script

  FixedNow=Now 'VBS1
  Wscript.Echo FixedNow 'VBS1
  '
  'The Second Visual Basic Script

  Set arg = WScript.Arguments 'VBS2
  NewDatetime = DateAdd("n", Eval(arg(0)), arg(1)) 'VBS2
  Wscript.StdOut.Write (DatePart("yyyy",NewDateTime)) & " " 'VBS2
  Wscript.StdOut.Write Right(0 & DatePart("m",NewDateTime), 2) & " " 'VBS2
  Wscript.StdOut.Write Right(0 & DatePart("d",NewDateTime), 2) & " " 'VBS2
  Wscript.StdOut.Write Right(0 & DatePart("h",NewDateTime), 2) & " " 'VBS2
  Wscript.StdOut.Write Right(0 & DatePart("n",NewDateTime), 2) & " " 'VBS2
  Wscript.StdOut.WriteLine Right(0 & DatePart("s",NewDateTime), 2) 'VBS2

The output could be e.g.
  C:\_D\TEST>cmdfaq -100
  DateTimeNow =17.04.2009 01:23:03
  yyyy_=2009
  mm_=04
  dd_=16
  hh_=23
  nn_=43
  ss_=03

Another example of the output (get a year from now)
  C:\_D\TEST>cmdfaq 365*24*60
  DateTimeNow =09.05.2009 11:44:31
  yyyy_=2010
  mm_=05
  dd_=09
  hh_=11
  nn_=44
  ss_=31

[Previous] [Next]

C:\_G\WWW\~ELISANET\INFO\tscmd147.html
C:\_G\WWW\~ELISANET\FTPCMD\TSALMI.CMD /tscmd147
http://www.elisanet.fi/tsalmi/info/tscmd147.html
file:///c:/_g/www/~elisanet/info/tscmd147.html