@echo off
rem fcpy.btm (file count per year)
rem usage: fcpy [/y:n] n=years back [> filename] filename= csv-file for excel
rem
rem - generating a statistical report of # files / size of files for a choosen directory
rem - values too old or in the future ( generated e.g. by misconfigured digicams ) are summarized
rem - uses "logparser" as an external utility
rem - the output can be used in ms excel ( convert text to columns )
rem frank, july 2012
setlocal
on break goto end
rem standard for back is 10 years. did user supply /y:n ?
set yearsback=10
if %@word[":",0,%1] eq /y set yearsback=%@word[":",1,%1]
rem array-index, 0-based
set /a lastindex=%yearsback + 1
rem number of elements, 1-based
set /a nelements=%yearsback + 2
set /a baseyear=%_year - %yearsback
unsetarray /q array
setarray array[%nelements,2]
unsetarray /q sumarray1
setarray sumarray1[2]
set dir=n:\dv\dlfj
rem @getdir returns empty string if the user selects "Cancel" or presses Esc
set dir=%@getdir[%dir]
if "%dir" eq "" (
echo no dir selected, quitting
goto end
)
rem generating header lines
echo Distribution of File-Age and -Size for Directory "%dir"
echo.
echos Dir,,older,,
for /L %y in (1,1,%@eval[%lastindex - 1]) echos %@eval[%baseyear + %y],,
echo future,,totals
echos ,# Files,MB,
for /L %y in (1,1,%@eval[%lastindex - 1]) echos # Files,MB,
echo # Files,MB,# Files,MB
setdos /x-56
rem first only the basedir
set subdir=%dir
gosub count -recurse:0
rem then all subdirs
do subdir in /o:n /a:d "%dir"\*.*
gosub count -recurse:-1
enddo
setdos /x0
:end
endlocal
quit
:count [ recurse ]
rem echoerr %subdir
rem if %@files["%subdir",d] eq 0 (
rem echo %subdir,
rem return
rem )
rem zeroing all array-elements
do loop = 0 to %lastindex
set array[%loop,0]=0
set array[%loop,1]=0
enddo
del /qe filecount_per_year.csv
rem logparser produces something like this (year,#files,size):
rem 1997,8,1820999
rem 1999,6,786109
rem 2002,174,100661945
rem 2003,188,132494748
rem 2008,113,1935912035
rem 2009,58,52290671
rem 2010,148,581229337
rem 2011,95,4742529049
rem 2012,105,718922410
logparser -i:fs %recurse %=
"select to_string(LastWriteTime,'yyyy') as year, count(*) as count, sum(size) as total %=
into filecount_per_year.csv %=
from '%subdir\*.*' %=
where attributes not like 'D%%' %=
group by year order by year" %=
-o:csv -q:on -filemode:0 -headers:off
if %? ne 0 (
echoerr pause fehler in "%subdir"
pause "%subdir"
)
set count=0
set tsize=0
set sumarray1[0]=0
set sumarray1[1]=0
do line in @filecount_per_year.csv
set year=%@word[",",0,%line]
set count=%@word[",",1,%line]
set tsize=%@word[",",2,%line]
set tsize=%@eval[ %tsize / 2**20]
set tsize=%@formatn[ .0,%tsize]
rem too old / too young ?
iff %year LE %baseyear then
rem older
set /a array[0,0]=%array[0,0] + %count
set /a array[0,1]=%array[0,1] + %tsize
elseiff %year GT %_year then
rem future dates
set /a array[%lastindex,0]=%array[%lastindex,0] + %count
set /a array[%lastindex,1]=%array[%lastindex,1] + %tsize
else
set /a aindex=%year - %baseyear
set array[%aindex,0]=%count
set array[%aindex,1]=%tsize
endiff
set /a sumarray1[0]=%sumarray1[0] + %count
set /a sumarray1[1]=%sumarray1[1] + %tsize
enddo
echos "%subdir",
do loop = 0 to %lastindex
echos %array[%loop,0],%array[%loop,1],
enddo
echos %sumarray1[0],%sumarray1[1]
echo.
return