DosTips.com ... for WinXP
Search:
Last update:
Dec 18, 2007

DOS Batch - Date and Time

Description

Date and Time functions are useful for:

The following example demonstrates how to use the date2jdate function to determine the age in days of the files in a temp directory.

Script Output

 DOS Script Ouput
00000002.ini is 42 days old
ActivePerlInstall.log is 39 days old
BatchJDate.bat is 0 days old
control.xml is 34 days old
debugf.txt is 26 days old
DFC5A2B2.TMP is 3 days old
EML30.tmp is 2 days old
EML39.tmp is 2 days old
EML3D.tmp is 2 days old
EXCEL.log is 20 days old
fdm9E1.tmp is 39 days old
gtb2C4.tmp is 62 days old
tmp.cab is 62 days old - gtb2
h2rC95.tmp is 36 days old
hpodvd09.log is 1 days old
hpzcoi00.log is 7 days old
hpzcoi01.log is 7 days old
hpzcoi02.log is 7 days old
hpzcoi03.log is 7 days old
IMT10.xml is 73 days old
IMT11.xml is 73 days old
IMT12.xml is 73 days old
IMT13.xml is 73 days old
IMT14.xml is 73 days old
IMT2B.xml is 73 days old
IMTF.xml is 73 days old
java_install_reg.log is 7 days old
jusched.log is 1 days old
LSBurnWatcher.log is 1 days old
msohdinh.tmp is 62 days old
patch.exe is 850 days old
patchw32.dll is 850 days old
r2hC94.tmp is 36 days old

Press any key to continue . . .
Download: BatchFTime.bat  

Script

  1. @echo off
  2. SETLOCAL ENABLEEXTENSIONS
  3. SETLOCAL ENABLEDELAYEDEXPANSION
  4. cd /d "%temp%"
  5. call:jdate tnow "%date%"
  6. for %%F in (*.*) do (
  7.     call:ftime tfile "%%F"
  8.     set /a diff=tnow-tfile
  9.     echo.%%~nxF is !diff! days old
  10. )
  11. ECHO.&PAUSE&GOTO:EOF
  12. ::-----------------------------------------------------------------------------------
  13. ::-- Functions start below here
  14. ::-----------------------------------------------------------------------------------
  15. :ftime JD filename attr -- compares the time of two files, succeeds if condition is met, fails otherwise
  16. ::                  -- JD    [out]    - valref file time in julian days
  17. ::                  -- attr  [in,opt] - time field to be used, creation/last-access/last-write, see 'dir /?', i.e. /tc, /ta, /tw, default is /tw
  18. :$created 20060101 :$changed 20080219
  19. :$source http://www.dostips.com
  20. SETLOCAL
  21. set file=%~2
  22. set attr=%~3
  23. if "%attr%"=="" set attr=/tw
  24. for /f %%a in ('"dir %attr% /-c "%file%"|findstr "^^[0-1]""') do set T=%%a
  25. call:jdate JD "%T%"
  26. ( ENDLOCAL & REM RETURN VALUES
  27.     IF "%~1" NEQ "" (SET %~1=%JD%) ELSE (echo.%JD%)
  28. )
  29. EXIT /b
  30. :jdate JD DateStr -- converts a date string to julian day number with respect to regional date format
  31. ::                -- JD      [out,opt] - julian days
  32. ::                -- DateStr [in,opt]  - date string, e.g. "03/31/2006" or "Fri 03/31/2006" or "31.3.2006"
  33. :$reference http://groups.google.com/group/alt.msdos.batch.nt/browse_frm/thread/a0c34d593e782e94/50ed3430b6446af8#50ed3430b6446af8
  34. :$created 20060101 :$changed 20080219
  35. :$source http://www.dostips.com
  36. SETLOCAL
  37. set DateStr=%~2&if "%~2"=="" set DateStr=%date%
  38. for /f "skip=1 tokens=2-4 delims=(-)" %%a in ('"echo.|date"') do (
  39.     for /f "tokens=1-3 delims=/.- " %%A in ("%DateStr:* =%") do (
  40.         set %%a=%%A&set %%b=%%B&set %%c=%%C))
  41. set /a "yy=10000%yy% %%10000,mm=100%mm% %% 100,dd=100%dd% %% 100"
  42. set /a JD=dd-32075+1461*(yy+4800+(mm-14)/12)/4+367*(mm-2-(mm-14)/12*12)/12-3*((yy+4900+(mm-14)/12)/100)/4
  43. ENDLOCAL & IF "%~1" NEQ "" (SET %~1=%JD%) ELSE (echo.%JD%)
  44. EXIT /b