Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for sorttime3.bat

(view source code of sorttime3.bat as plain text)

  1. @ECHO OFF
  2. :: Windows 2000 and later only
  3. IF NOT "%OS%"=="Windows_NT" GOTO OsErr
  4.  
  5. :: Remove the following line to use this batch file
  6. :: in Windows NT 4 with the Resource Kit installed
  7. VER | FIND "Windows NT" >NUL && GOTO OsErr
  8.  
  9. :: Help required?
  10. IF NOT "%~1"=="" GOTO Syntax
  11.  
  12. :: Enable Command Extensions and use local variables
  13. VERIFY OTHER 2>nul
  14. SETLOCAL ENABLEEXTENSIONS
  15. IF ERRORLEVEL 1 GOTO CmdExtErr
  16.  
  17. :: Export registry settings to a temporary file
  18. START /W REGEDIT /E "%Temp%.\_SortTime.reg" "HKEY_CURRENT_USER\Control Panel\International"
  19. :: Read the time format from the exported data
  20. FOR /F "tokens=1* delims==" %%A IN ('TYPE "%Temp%.\_SortTime.reg" ^| FINDSTR /R /I /B /C:"\"iTime\"="') DO SET iTime=%%~B
  21. :: Thanks for Daniel R. Foster for correcting a typo in the next line
  22. FOR /F "tokens=1* delims==" %%A IN ('TYPE "%Temp%.\_SortTime.reg" ^| FINDSTR /R /I /B /C:"\"sTime\"="') DO SET sTime=%%~B
  23. DEL "%Temp%.\_SortTime.reg"
  24.  
  25. :: Store current time in default Locale format
  26. FOR /F "tokens=*" %%A IN ('TIME/T') DO SET Now=%%A
  27. ECHO It's %Now% now
  28.  
  29. :: Format SortTime depending on registry settings
  30. FOR /F "tokens=1,2* delims=%sTime% " %%A IN ('ECHO %Now%') DO (
  31. 	SET Hour=%%A
  32. 	SET Mins=%%B
  33. )
  34. :: Translate AM/PM to 24 hours format;
  35. :: A correction was required for Windows XP,
  36. :: thanks for Harry Teufel for finding this bug
  37. ECHO.%Mins%| FIND /I "AM" >NUL && SET AmPm=A
  38. ECHO.%Mins%| FIND /I "PM" >NUL && SET AmPm=P
  39. :: AM/PM can be all upper or mixed case, with or without leading space; we'll just erase all possible characters
  40. SET Mins=%Mins: =%
  41. SET Mins=%Mins:A=%
  42. SET Mins=%Mins:a=%
  43. SET Mins=%Mins:M=%
  44. SET Mins=%Mins:m=%
  45. SET Mins=%Mins:P=%
  46. SET Mins=%Mins:p=%
  47. IF 1%Hour% LSS 20 SET Hour=0%Hour%
  48. IF 1%Mins% LSS 20 SET Mins=0%Mins%
  49. IF %Hour% LSS 12 IF /I "%AmPm%"=="P" SET /A Hour=1%Hour%-88
  50. IF %Hour% EQU 12 IF /I "%AmPm%"=="A" SET    Hour=00
  51. SET Now=%Hour%%sTime%%Mins%
  52.  
  53. :Sort
  54. FOR /F "tokens=1,2* delims=%sTime% " %%A IN ('ECHO %Now%') DO SET SortTime=%%A%%B
  55. :: Thanks for Holger Stein who mailed me this correction (add leading zero):
  56. IF %SortTime% LSS 1000 SET SortTime=0%SortTime%
  57. ECHO For sorting purposes: %SortTime%
  58.  
  59. :: Purge local variables except SortTime
  60. ENDLOCAL & SET SortTime=%SortTime%
  61. GOTO End
  62.  
  63. :CmdExtErr
  64. ECHO.
  65. ECHO Command extensions need to be enabled for this batch file to run correctly.
  66. ECHO.
  67. ECHO You can run this batch file using the command:
  68. ECHO.
  69. ECHO     CMD /X %~n0
  70. ECHO.
  71. ECHO to enable command extensions, however, the results will not be saved in
  72. ECHO environment variables that way.
  73. GOTO Syntax
  74.  
  75. :OsErr
  76. ECHO.
  77. ECHO This batch file requires Windows 2000 or a later version!
  78. ECHO.
  79. ECHO To use this batch file with Windows NT 4 you need to install FINDSTR from
  80. ECHO the Resource Kit, and to remove the NT 4 check from this batch file.
  81. ECHO Read the comment lines in this batch file for details.
  82.  
  83. :Syntax
  84. ECHO.
  85. ECHO SortTime.bat,  Version 3.40 for Windows 2000 / XP
  86. ECHO Displays the time of execution in the system's default time format and in
  87. ECHO hhmm format for sorting purposes.
  88. ECHO.
  89. ECHO Usage:  SORTTIME
  90. ECHO.
  91. ECHO Notes:  [1]  The sorted value is stored in the environment variable SORTTIME.
  92. ECHO              The SORTTIME value is independent of "International", "Regional"
  93. ECHO              or "Locale" settings.
  94. ECHO         [2]  This batch file uses native Windows 2000 commands only.
  95. ECHO              To allow the use in Windows NT 4, make sure FINDSTR from the
  96. ECHO              Resource Kit is installed, and remove the NT 4 check from this
  97. ECHO              batch file. Read the comment lines for details.
  98. ECHO         [3]  KiXtart, Perl, JScript, Rexx and VBScript versions are available
  99. ECHO              at http://www.robvanderwoude.com/datetimenonbatch.html
  100. ECHO.
  101. ECHO Written by Rob van der Woude
  102. ECHO http://www.robvanderwoude.com
  103. ECHO ^(with corrections by Daniel R. Foster, Harry Teufel and Holger Stein^)
  104. ECHO.
  105.  
  106. :End
  107. :: Done
  108.  

page last modified: 2024-04-16; loaded in 0.0090 seconds