Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for sortdate4.bat

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

  1. @ECHO OFF
  2. CLS
  3. ECHO.
  4. ECHO SortDate.bat, Version 4.01 for Windows NT 4 / 2000 / XP
  5. ECHO Display day, month and year, independent of Windows'
  6. ECHO Regional Settings, and using internal commands only.
  7. ECHO.
  8. ECHO Based on Simon Sheppard's GetDate.bat
  9. ECHO http://www.ss64.com/ntsyntax/GetDate.txt
  10. ECHO.
  11. ECHO This version demonstrates how to adjust the code for non-English Windows
  12. ECHO.
  13. ECHO Written by Rob van der Woude
  14. ECHO http://www.robvanderwoude.com
  15. ECHO.
  16.  
  17. SETLOCAL
  18. :: Get the format: dd-mm-yy, or mm/dd/yy, or whatever other format your PC uses
  19. FOR /F "tokens=2 delims=()" %%A IN ('ECHO. ^| DATE') DO SET Format=%%A
  20. :: Get the delimiter used: the first character that is different
  21. SET Char1=%Format:~0,1%
  22. SET Char2=%Format:~1,1%
  23. SET Char3=%Format:~2,1%
  24. SET Char4=%Format:~3,1%
  25. SET Char5=%Format:~4,1%
  26. IF NOT [%Char1%]==[%Char2%] (
  27. 	SET Delim=%Char2%
  28. 	GOTO Parse
  29. )
  30. IF NOT [%Char2%]==[%Char3%] (
  31. 	SET Delim=%Char3%
  32. 	GOTO Parse
  33. )
  34. IF NOT [%Char3%]==[%Char4%] (
  35. 	SET Delim=%Char4%
  36. 	GOTO Parse
  37. )
  38. IF NOT [%Char4%]==[%Char5%] (
  39. 	SET Delim=%Char5%
  40. ) ELSE (
  41. 	ECHO Error finding delimiter.
  42. 	ECHO Aborting . . .
  43. 	GOTO:EOF
  44. )
  45.  
  46. :: Get the current date string
  47. :Parse
  48. FOR /F "tokens=1* delims= " %%A IN ('DATE/T') DO IF "%%B"=="" (SET Date=%%A) ELSE (SET Date=%%B)
  49.  
  50. :: Display the intermediate results
  51. ECHO Date=%Date%        Format=%Format%        Delim=%Delim%
  52. ECHO.
  53.  
  54. :: Parse the Date string using the delimiter found earlier
  55. FOR %%? IN (1,2,3) DO CALL :ParseVal %%?
  56.  
  57. :: This assumes the variable names are dd, mm and yy
  58. ECHO.
  59. ECHO For English NT versions only, modify if format does NOT use dd, mm and yy.
  60. ECHO SET SortDate=%%yy%%%%mm%%%%dd%%
  61. ECHO.
  62. SET SortDate=%yy%%mm%%dd%
  63. SET SortDate
  64.  
  65. ENDLOCAL & SET SortDate=%SortDate%
  66. GOTO:EOF
  67.  
  68.  
  69. :ParseVal
  70. :: Get the day, month or year variable name
  71. FOR /F "tokens=%1 delims=%Delim% " %%A IN ('ECHO.%Format%') DO SET Var%1=%%A
  72. :: Get the day, month or year variable value
  73. FOR /F "tokens=%1 delims=%Delim% " %%A IN ('ECHO.%Date%')   DO SET Val%1=%%A
  74. :: Assingn the value
  75. CALL SET %%Var%1%%=%%Val%1%%
  76. :: Display the result
  77. CALL SET %%Var%1%%
  78. GOTO:EOF
  79.  

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