Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for sorttime2.bat

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

  1. @ECHO OFF
  2. :: Help required?
  3. IF NOT [%1]==[] GOTO Syntax
  4.  
  5. :: Windows NT 4/2000 only
  6. SET OS_OK=0
  7. VER | FIND "Windows NT" >NUL
  8. IF NOT ERRORLEVEL 1 SET OS_OK=1
  9. VER | FIND "Windows 2000" >NUL
  10. IF NOT ERRORLEVEL 1 SET OS_OK=1
  11. IF [%OS_OK%]==[0] GOTO :OsErr
  12. :: Command Extensions should be enabled
  13. SET /A 12 + 4 2>&1 | FIND "16" >NUL
  14. IF ERRORLEVEL 1 GOTO :CmdExtErr
  15.  
  16. :: Store current time in default Locale format
  17. FOR /F "TOKENS=*" %%A IN ('TIME/T') DO SET TIME=%%A
  18. ECHO It's %TIME% now
  19.  
  20. :: Store current time in a variable in hh:mm format
  21. :: Export registry settings to a temporary file
  22. START /W REGEDIT /E %TEMP%.\_TEMP.REG "HKEY_CURRENT_USER\Control Panel\International"
  23. :: Read the exported data, filtering out keys added by Windows 2000
  24. FOR /F "tokens=1* delims==" %%A IN ('TYPE %TEMP%.\_TEMP.REG ^| FIND "iTime" ^| FIND /V "iTimePrefix"') DO SET iTime=%%B
  25. :: Thanks for Daniel R. Foster for correcting a typo in the next line
  26. FOR /F "tokens=1* delims==" %%A IN ('TYPE %TEMP%.\_TEMP.REG ^| FIND "sTime" ^| FIND /V "sTimeFormat"') DO SET sTime=%%B
  27. DEL %TEMP%.\_TEMP.REG
  28. :: Remove quotes
  29. SET iTime=%iTime:"=%
  30. SET sTime=%sTime:"=%
  31.  
  32. :: Format SORTTIME depending on registry settings
  33. IF %iTime%==1 GOTO Sort
  34. SET AMPMTIME=%TIME%
  35. FOR /F "TOKENS=1,2* DELIMS=%sTime% " %%A IN ('ECHO %TIME%') DO (
  36. 	SET HOUR=%%A
  37. 	SET MINS=%%B
  38. )
  39. SET AMPM=%MINS:~2,1%
  40. SET MINS=%MINS:~0,2%
  41. IF %HOUR% LSS 12 IF /I %AMPM%==P SET /A HOUR=%HOUR%+12
  42. IF %HOUR% LSS 10 IF /I %AMPM%==A SET HOUR=0%HOUR%
  43. IF %HOUR% EQU 12 IF /I %AMPM%==A SET HOUR=00
  44. SET TIME=%HOUR%%sTime%%MINS%
  45.  
  46. :Sort
  47. FOR /F "TOKENS=1,2* DELIMS=%sTime% " %%A IN ('ECHO %TIME%') DO SET SORTTIME=%%A%%B
  48. :: Thanks for Holger Stein who mailed me this correction (add leading zero):
  49. IF %SORTTIME% LSS 1000 SET SORTTIME=0%SORTTIME%
  50. ECHO For sorting purposes: %SORTTIME%
  51. GOTO End
  52.  
  53. :CmdExtErr
  54. ECHO.
  55. ECHO Command extensions need to be enabled for this batch file to run correctly.
  56. ECHO.
  57. ECHO You can run this batch file using the command:
  58. ECHO.
  59. ECHO     CMD /X %~n0
  60. ECHO.
  61. ECHO to enable command extensions, however, the results will not be saved in
  62. ECHO environment variables that way.
  63. GOTO Syntax
  64.  
  65. :OsErr
  66. ECHO.
  67. ECHO This batch file is written for Windows NT 4 and Windows 2000 only!
  68. ECHO.
  69. ECHO Visit http://www.robvanderwoude.com for Kix and Rexx versions.
  70.  
  71. :Syntax
  72. ECHO.
  73. ECHO SortTime.bat,  Version 3.12 for Windows NT 4 and Windows 2000
  74. ECHO Displays the time of execution in the system's default time format and in
  75. ECHO hhmm format for sorting purposes.
  76. ECHO The values are also stored in the environment variables DATE and SORTDATE.
  77. ECHO This version uses native Windows commands only.
  78. ECHO.
  79. ECHO Written by Rob van der Woude
  80. ECHO http://www.robvanderwoude.com
  81. ECHO.
  82. ECHO Usage:  %~n0
  83.  
  84. :End
  85. SET OS_OK=
  86.  

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