Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for sortdate2.bat

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

  1. @ECHO OFF
  2. :: Windows NT 4 or later only
  3. IF NOT "%OS%"=="Windows_NT" GOTO Syntax
  4. :: No command line arguments required
  5. IF NOT [%1]==[] GOTO Syntax
  6.  
  7. :: Keep variables local
  8. SETLOCAL
  9.  
  10. :: Store current date in a variable in default Locale format
  11. FOR /F "TOKENS=1* DELIMS= " %%A IN ('DATE/T') DO SET DATE=%%B
  12. ECHO It is %DATE% today
  13.  
  14. :: Store current date in a variable in YYYYMMDD format
  15. :: Export registry settings to a temporary file
  16. START /W REGEDIT /E %TEMP%.\_TEMP.REG "HKEY_CURRENT_USER\Control Panel\International"
  17. :: Read the exported data
  18. FOR /F "tokens=1* delims==" %%A IN ('TYPE %TEMP%.\_TEMP.REG ^| FIND /I "iDate"') DO SET iDate=%%B
  19. FOR /F "tokens=1* delims==" %%A IN ('TYPE %TEMP%.\_TEMP.REG ^| FIND /I "sDate"') DO SET sDate=%%B
  20. DEL %TEMP%.\_TEMP.REG
  21. :: Remove quotes
  22. SET iDate=%iDate:"=%
  23. SET sDate=%sDate:"=%
  24.  
  25. :: Parse today's date depending on registry's date format settings
  26. IF %iDate%==0 FOR /F "TOKENS=1-4* DELIMS=%sDate%" %%A IN ('DATE/T') DO (
  27. 	SET Year=%%C
  28. 	SET Month=%%A
  29. 	SET Day=%%B
  30. )
  31. IF %iDate%==1 FOR /F "TOKENS=1-4* DELIMS=%sDate%" %%A IN ('DATE/T') DO (
  32. 	SET Year=%%C
  33. 	SET Month=%%B
  34. 	SET Day=%%A
  35. )
  36. IF %iDate%==2 FOR /F "TOKENS=1-4* DELIMS=%sDate%" %%A IN ('DATE/T') DO (
  37. 	SET Year=%%A
  38. 	SET Month=%%B
  39. 	SET Day=%%C
  40. )
  41. :: Remove the day of week if applicable
  42. FOR %%A IN (%Year%)  DO SET Year=%%A
  43. FOR %%A IN (%Month%) DO SET Month=%%A
  44. FOR %%A IN (%Day%)   DO SET Day=%%A
  45.  
  46. :: Today's date in YYYYMMDD format
  47. SET SortDate=%Year%%Month%%Day%
  48. ECHO For sorting purposes: %SortDate%
  49.  
  50. :: Done
  51. ENDLOCAL & SET SortDate=%SortDate%
  52. GOTO:EOF
  53.  
  54.  
  55. :Syntax
  56. ECHO.
  57. ECHO SortDate, Version 3.10 for Windows NT 4 / 2000 / XP
  58. ECHO Save date and "sorted" date in environment variables.
  59. ECHO.
  60. ECHO Usage:  %0
  61. ECHO.
  62. ECHO This batch file will always display the same results,
  63. ECHO independent of "International" settings.
  64. ECHO.
  65. ECHO Written by Rob van der Woude
  66. ECHO http://www.robvanderwoude.com
  67. ECHO Adapted for Windows XP with help from Kailash Chanduka
  68.  

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