Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for datefmt.bat

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

  1. @ECHO OFF
  2. IF NOT "%OS%"=="Windows_NT" GOTO Syntax
  3. IF      "%~1"==""           GOTO Syntax
  4. IF NOT  "%~6"==""           GOTO Syntax
  5. ECHO.%* | FIND "?" > NUL && GOTO Syntax
  6.  
  7. SETLOCAL ENABLEDELAYEDEXPANSION
  8. FOR /F "tokens=* delims=" %%A IN ('VER ^| DATE') DO (
  9. 	REM Save only last word in variable
  10. 	FOR %%B IN (%%A) DO SET DateFmt=%%B
  11. 	FOR /F "tokens=1-3 delims=(/-)" %%B IN ("!DateFmt!") DO (
  12. 		SET Var1=%%B
  13. 		SET Var2=%%C
  14. 		SET Var3=%%D
  15. 	)
  16. )
  17. FOR /F "tokens=* delims=" %%A IN ('VER ^| DATE ^| FIND /V "("') DO (
  18. 	REM Save only last word in variable, thereby ignoring optional
  19. 	REM leading day of week, error reported by Fernando Sader
  20. 	FOR %%B IN (%%A) DO SET Today=%%B
  21. 	FOR /F "tokens=1-3 delims=./- " %%B IN ("!Today!") DO (
  22. 		SET %Var1%=%%B
  23. 		SET %Var2%=%%C
  24. 		SET %Var3%=%%D
  25. 	)
  26. )
  27.  
  28. IF /I NOT "%~1"=="%Var1%" IF /I NOT "%~1"=="%Var2%" IF /I NOT "%~1"=="%Var3%" (
  29. 	ENDLOCAL
  30. 	GOTO Syntax
  31. )
  32. IF /I "%~4"=="/LZ" (SET Delim=) ELSE (SET Delim=%4)
  33. IF /I NOT "%~3"=="%Var1%" IF /I NOT "%~3"=="%Var2%" IF /I NOT "%~3"=="%Var3%" IF /I NOT "%~3"=="/LZ" (SET Delim=%~3)
  34. IF /I NOT "%~2"=="%Var1%" IF /I NOT "%~2"=="%Var2%" IF /I NOT "%~2"=="%Var3%" IF /I NOT "%~2"=="/LZ" (SET Delim=%~2)
  35. ECHO.%* | FIND /I "/LZ" >NUL
  36. IF NOT ERRORLEVEL 1 CALL :AddLeadingZero
  37. SET DateFmt=!%1!
  38. IF /I NOT "%~2"=="%Delim%" IF /I NOT "%~2"=="/LZ" (SET DateFmt=%DateFmt%%Delim%!%2!)
  39. IF /I NOT "%~3"=="%Delim%" IF /I NOT "%~3"=="/LZ" (SET DateFmt=%DateFmt%%Delim%!%3!)
  40. ENDLOCAL & SET DateFmt=%DateFmt%
  41.  
  42. SET DateFmt
  43.  
  44. GOTO:EOF
  45.  
  46.  
  47. :AddLeadingZero
  48. CALL SET Char1=%%%Var1%:~0,1%%
  49. IF NOT "%Char1%"=="0" (
  50. 	IF !%Var1%! LSS 10 SET %Var1%=0!%Var1%!
  51. )
  52. CALL SET Char1=%%%Var2%:~0,1%%
  53. IF NOT "%Char1%"=="0" (
  54. 	IF !%Var2%! LSS 10 SET %Var2%=0!%Var2%!
  55. )
  56. CALL SET Char1=%%%Var3%:~0,1%%
  57. IF NOT "%Char1%"=="0" (
  58. 	IF !%Var3%! LSS 10 SET %Var3%=0!%Var3%!
  59. )
  60. GOTO:EOF
  61.  
  62.  
  63. :Syntax
  64. ECHO DateFmt.bat,  Version 0.53 BETA for Windows NT 4 and later
  65. ECHO Display the current date in the specified format
  66. ECHO.
  67. ECHO Usage:  DATEFMT  date_format  [ delimiter ]  [ /LZ ]
  68. ECHO.
  69. IF     "%OS%"=="Windows_NT" FOR /F "tokens=2-4 delims=()/-" %%A IN ('VER ^| DATE ^| FIND "("') DO ECHO Where:  date_format is any combination of %%A, %%B and/or %%C
  70. IF NOT "%OS%"=="Windows_NT" ECHO Where:  date_format is any combination of dd, mm and/or yy
  71. ECHO                     (these date_format options are always in the computer's
  72. IF NOT "%OS%"=="Windows_NT" ECHO                     local language; to look them up, type VER ³ DATE)
  73. IF NOT "%OS%"=="Windows_NT" GOTO Skip
  74. ECHO                     local language; to look them up, type VER ^| DATE)
  75. :Skip
  76. ECHO         delimiter   is the delimiter to be used in the end result
  77. ECHO         /LZ         use leading zeroes in the end result
  78. ECHO.
  79. ECHO Examples (for English Windows versions):
  80. ECHO DATEFMT yy mm dd        ---  2007115    (January 15 or November 5, 2007)
  81. ECHO DATEFMT yy mm dd -      ---  2007-11-5  (November 5, 2007)
  82. ECHO DATEFMT yy mm dd - /LZ  ---  2007-11-05 (November 5, 2007)
  83. ECHO DATEFMT mm /LZ          ---  01         (January)
  84. ECHO DATEFMT yy mm - /LZ     ---  2007-06    (June 2007)
  85. ECHO DATEFMT dd mm dd * /LZ  ---  11*03*11   (March 11)
  86. ECHO.
  87. ECHO Inspired by Simon Sheppard's GetDate.bat
  88. ECHO http://www.ss64.com/ntsyntax/getdate.html
  89. ECHO Written by Rob van der Woude
  90. ECHO http://www.robvanderwoude.com
  91.  

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