Skip navigation

JSI Tip 8327. How do I count the number of files on a drive, or folder, by year?


I have scripted FileAgeSummary.bat to count the number of files on a drive, or folder, by year.

The syntax for using FileAgeSummary.bat is:

FileAgeSummary DriveOrFolder \[DIR Switches like /S /TC\]

Where DriveOrFolder is the drive or folder you wish to enumerate, and DIR Switches like /S /TC are optional DIR switchs, like /S to enumerate sub-folders, and /TC to use the file Creation date instead of the default Last Written date, per the following from DIR /?:

/T          Controls which time field displayed or used for sorting
timefield   C  Creation
            A  Last Access
            W  Last Written
To count all the files on the C: drive, by year of creation, use:

FileAgeSummary C: /S /TC

The output is displayed on the CMD.EXE console, and might look like:

1980 31
1995 17
1996 102
1997 32
1998 822
1999 290
2000 523
2001 910
2002 7973
2003 41833
2004 19004
NOTE: See How can I find files that were created, or written, or accessed, in a given year?

FileAgeSummary.bat contains:

@echo off
if \{%1\}

\{\} @echo Syntax: FileAgeSummary DriveOrFolder \[DIR Switches like /S /TC\]&goto :EOF if not exist %1 @echo FileAgeSummary - %1 NOT found.&goto :EOF setlocal set folder=%1 set $tok=1-3 for /f "tokens=1 delims=.:/-, " %%u in ('date /t') do set $d1=%%u if "%$d1:~0,1%" GTR "9" set $tok=2-4 for /f "tokens=%$tok% delims=.:/-, " %%u in ('date /t') do ( for /f "skip=1 tokens=2-4 delims=/-,()." %%x in ('echo.^|date') do ( set %%x=%%u set %%y=%%v set %%z=%%w call set year=%%%%z%% set $d1= set $tok=)) set low=%year% set d1=dir %folder% /a /a-d :swloop if \{%2\}

\{\} goto swend set d1=%d1% %2 shift goto swloop :swend set f1=findstr /L /C:"/" for /f "Tokens=3 Delims=/ " %%a in ('%d1%^|%f1%') do ( call :age %%a ) for /l %%a in (%low%,1,%year%) do ( if defined Y%%a call :report %%a ) endlocal goto :EOF :age if not defined Y%1 set /a Y%1=0 call set /a Y%1=%%Y%1%% + 1 if "%1" LSS "%low%" set low=%1 goto :EOF :report call set /a work=%%Y%1%% @echo %1 %work%



Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish