File age in days

Convert the file date into Julian Days to determine the age of a file age in days.

Description:

Date and Time functions are useful for:

  • Calculations with date and time values
  • Determine the age of files in days
  • Determine the date difference in days

The example in this section demonstrates how to use the :ftime function to determine the age in days of all files in the temp directory.

Two variables are used

  • tnow - stores the current day in julian days format by calling :jdate
  • tfile - stores the file date in julian days format by calling :ftime

Using Delayed Expansion and exclamation marks around environment variables ensures that the `tfile`variable is substituted properly during each loop. Read more about this behavior in the SET command help (bottom half of the help text).

Script: Download: BatchFTime.bat  
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
@echo off
SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION

cd /d "%temp%"

call:jdate tnow "%date%"
for %%F in (*.*) do (
    call:ftime tfile "%%F"
    set /a diff=tnow-tfile
    echo.%%~nxF is !diff! days old
)

ECHO.&PAUSE&GOTO:EOF


::-----------------------------------------------------------------------------------
::-- Functions start below here
::-----------------------------------------------------------------------------------


:ftime JD filename attr -- returns the file time in julian days
::                      -- JD    [out]    - valref file time in julian days
::                      -- attr  [in,opt] - time field to be used, creation/last-access/last-write, see 'dir /?', i.e. /tc, /ta, /tw, default is /tw
:$created 20060101 :$changed 20090322 :$categories DateAndTime
:$source https://www.dostips.com
SETLOCAL
set file=%~2
set attr=%~3
if not defined attr (call:jdate JD "- %~t2"
) ELSE (for /f %%a in ('"dir %attr% /-c "%file%"|findstr "^^[0-9]""') do call:jdate JD "%%a")
( ENDLOCAL & REM RETURN VALUES
    IF "%~1" NEQ "" (SET %~1=%JD%) ELSE (echo.%JD%)
)
EXIT /b


:jdate JD DateStr -- converts a date string to julian day number with respect to regional date format
::                -- JD      [out,opt] - julian days
::                -- DateStr [in,opt]  - date string, e.g. "03/31/2006" or "Fri 03/31/2006" or "31.3.2006"
:$reference https://groups.google.com/group/alt.msdos.batch.nt/browse_frm/thread/a0c34d593e782e94/50ed3430b6446af8#50ed3430b6446af8
:$created 20060101 :$changed 20080219
:$source https://www.dostips.com
SETLOCAL
set DateStr=%~2&if "%~2"=="" set DateStr=%date%
for /f "skip=1 tokens=2-4 delims=(-)" %%a in ('"echo.|date"') do (
    for /f "tokens=1-3 delims=/.- " %%A in ("%DateStr:* =%") do (
        set %%a=%%A&set %%b=%%B&set %%c=%%C))
set /a "yy=10000%yy% %%10000,mm=100%mm% %% 100,dd=100%dd% %% 100"
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
ENDLOCAL & IF "%~1" NEQ "" (SET %~1=%JD%) ELSE (echo.%JD%)
EXIT /b
Script Output:
 DOS Script Output
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 . . .