DISKTREND.CMD

:: Script created by Paul Kilian

:: Any queries regarding this script should be sent to 
paul@paulslore.com 

@echo off
setlocal ENABLEDELAYEDEXPANSION

set curdir=%cd%
:: Get date
set today=%date:~4%

:: Get parameters from INI file
echo Reading configuration
for /f "eol=; tokens=1,2 delims==" %%a in (disktrend.ini) do set %%a=%%b
if "!DataDir!" == "" set DataDir=Data
if "!DataDir:~-1!" == "\" set DataDir=!DataDir:~0,-1!
if "!ReportFile!" == "" set ReportFile=disktrend.html
if "!ReportPath!" == "" set ReportPath=Disk Trend Report
if "!ReportPath:~-1!" == "\" set ReportPath=!ReportPath:~0,-1!
if not exist "!DataDir!" md "!DataDir!"||(echo Error creating data directory&goto end)
if not exist "!ReportPath!" md "!ReportPath!"||(echo Error creating report directory&goto end)
if not exist "!ReportPath!\images" md "!ReportPath!\images"||(echo Error creating images directory&goto end)
if "!CustomTitle!" == "" (
	if "!AlternateDomain!" == "" (
		set CustomTitle=Analysis Report for %USERDOMAIN%
	) else set CustomTitle=Analysis Report for !AlternateDomain!
) else (
	for %%m in (!CustomTitle!) do (
		set tmpvar=%%m
		if "!tmpvar!" == "%%USERDOMAIN%%" (
			set Title=!Title! %USERDOMAIN%
		) else if "!tmpvar!" == "%%USERDNSDOMAIN%%" (
			set Title=!Title! %USERDNSDOMAIN%
		) else set Title=!Title! %%m
	)
	set CustomTitle=!Title!
)
if not exist "!CSSFile!" echo WARNING^^! - the CSS file does not exist
for %%a in (*.gif) do if not exist "!ReportPath!\images\%%a" copy /y %%a "!ReportPath!\images\">nul
for %%a in (*.css) do if not exist "!ReportPath!\%%a" copy /y %%a "!ReportPath!">nul

if not exist "!plpath!pl.exe" echo ERROR^^! - Ploticus executable not found&goto end
for /f "tokens=3,4 delims=/ " %%a in ("%date%") do (
	if %%a == 01 (
		set month=12
		set /a year=%%b-1
	) else (
		set /a month=1%%a-101
		set year=%%b
	)
)
if !month! LSS 10 set month=0!month!
set repmnth=!year!-!month!

if not "%1" == "" (
	if /i "%1" == "-graphonly" (
		set growthswitch=nogrowth
		goto graph
	) else if /i "%1" == "-graphonly" (
		goto graph
	) else if /i "%1" == "-growthonly" (
		goto growth
	) else if /i "%1" == "-nogrowth" (
		set growthswitch=nogrowth
	) else goto starterror
)

echo Reading data
if "!UseLocal!" == "YES" (
	echo Building server list
	if exist servers.tmp del servers.tmp
	if "!AlternateDomain!" == "" (
		for /f "tokens=1 delims=\ " %%a in ('net view ^|find "\\"') do >>servers.tmp echo %%a
	) else (
		for /f "tokens=1 delims=\ " %%a in ('net view /domain:!AlternateDomain!^|find "\\"') do >>servers.tmp echo %%a
	)
	if not exist servers.tmp (
		echo There was an error enumerating the server names. No servers were found.>"!ReportPath!!ReportFile!"
		goto end
	) else (
		if not "!UseFilter!" == "" (
			if "!UseFilter:~0,1!" == "-" (
				>servers.txt type servers.tmp|find /i /v "!UseFilter:~1!"
			) else >servers.txt type servers.tmp|find /i "!UseFilter!"
		) else copy /y servers.tmp servers.txt>nul
		del servers.tmp
	)
)

echo Gathering data from hosts

for /f %%a in (servers.txt) do srvinfo -ns \\%%a|findstr "Drive: $">"!DataDir!\%%a.srv"
for %%a in ("!DataDir!\*.srv") do (
	for /f  %%b in ('type "%%a"^|find /c "$"') do if %%b GTR 0 (
		set host=%%a
		set host=!host:~0,-4!
		set tempvar=!today!
		set cols=Date

		for /f "usebackq skip=1 tokens=1-5 delims=	 " %%m in ("%%a") do (
			set drv=%%m&set drv=!drv:~0,1!
			set cols=!cols!	!drv!-Total	!drv!-Free	!drv!-Used	!drv!-%%%%Free
			for /f "tokens=1,2 delims=." %%t in ('clc %%p/%%o*100') do set percent=%%u&set percent=%%t.!percent:~1,2!
			set tempvar=!tempvar!	%%o	%%p	%%q	!percent!
		)
		if not exist "!host!.tsv" echo !cols!>"!host!.tsv"
		find "!today!" "!host!.tsv">nul||echo !tempvar!>>"!host!.tsv"
	)
)
:graph
echo Generating daily graphs

echo Reading data
for /f %%a in ('dir /b "!DataDir!\*.tsv"^|find /v "~"^|find /v "Growth-"') do (
	set host=%%a
	set host=!host:~0,-4!
	set daycount=0
	set linedate=
	set count=0
	for /f "tokens=* delims=	" %%b in ('type "!DataDir!\%%a"^|findstr /i "Date"') do call :getlines %%b
	call :getrange !colcount! "!DataDir!\%%a"
	(
		echo #proc page
		echo #if @DEVICE in png,gif
		echo scale: 0.9
		echo backgroundcolor: white
		echo #endif
		echo.
		echo #proc getdata
		echo 	file: !DataDir!\%%a
		echo 	delimit: tab
		echo 	fieldnames: !fieldnames!
		echo.
		echo #proc areadef
		echo 	title: Disk Utilisation - !host!
		echo 	titledetails: size=14  align=C adjust=0,0.2
		echo 	rectangle: 1 1 8 4
		echo 	areacolor: gray^(0.8^)
		echo 	frame: width=1
		echo 	xscaletype: date dd/mm/yyyy
		echo 	xrange: !startdate! !linedate!+2
		echo 	yrange: 0 100+3
		echo.
		echo #proc xaxis
		echo 	stubs: inc 1 month
		echo 	minorticinc: 7 days
		echo 	stubformat: Mmm
		echo.
		echo #proc yaxis
		echo 	label: Percent Available
		echo 	stubs: inc 10
		echo 	gridskip: min
		echo 	ticincrement: 10 100
		echo	grid: color=gray^(0.7^)
		echo.
		for %%z in (!lines!) do (
			set /a count=!count+1
			echo #proc lineplot
			echo 	xfield: Date
			echo 	yfield: %%z
			if !count! == 1 set color=red
			if !count! == 2 set color=brightblue
			if !count! == 3 set color=brightgreen
			if !count! == 4 set color=yellow
			if !count! == 5 set color=lightpurple
			if !count! == 6 set color=orange
			if !count! == 7 set color=powderblue
			if !count! == 8 set color=coral
			if !count! == 9 set color=green
			echo 	linedetails: color=!color! width=1.5
			echo 	legendlabel: %%z
			echo.
		)

		echo #proc legend
		echo 	format: singleline
		echo 	location: min+0.2 min-0.4
		echo 	outlinecolors: yes
		echo 	sep 0.5
		echo 	seglen: 0.2
	)>"!DataDir!\temp.gra"
	"!plpath!pl" "!DataDir!\temp.gra" -gif -o "!ReportPath!\images\!host!.gif"
)
del "!DataDir!\temp.gra"

echo Building daily graph report
(
	echo ^<html^>
	echo ^<head^>
	echo 	^<title^>Disk Trend !CustomTitle!^</title^>
	echo 	^<link href="!CSSFile!" rel="stylesheet" type="text/css"^>
	echo ^</head^>
	echo ^<body^>
	echo ^<table width="100%%"^>
	echo 	^<tr^>
	echo 		^<td width="10"^>
	echo 		^<td align="left"^>Disk Trend !CustomTitle!^</td^>
	echo 		^<td align="right" valign="top" width="30%%"^>Last updated at %time:~0,-6% on %date%^</td^>
	echo 	^</tr^>
	echo ^</table^>
	echo ^<center^>
	echo ^<table^>
)>"!ReportPath!\default.htm"
set count=0
for /f %%a in ('dir /b "!ReportPath!\images\*.gif"^|find /v /i "growth"') do (
	if not !count! == 0 (
		(
			echo 	^<tr height="10"^>
			echo 		^<td colspan="3"^>^ ^</td^>
			echo 	^</tr^>
		)>>"!ReportPath!\default.htm"
	)
	(
		echo 	^<tr^>
		echo 		^<td colspan="2"^>^</td^>
		echo 		^<td^>
		echo 	^</tr^>
		echo 	^<tr^>
		echo 		^<td^>^</td^>
		echo 		^<td align="center"^>^<img src="images/%%a"^>^</td^>
		echo 		^<td^>^</td^>
		echo 	^</tr^>
		echo 	^<tr^>
		echo 		^<td^>^</td^>
		echo 		^<td^>^ ^</td^>
		echo 		^<td^>^</td^>
		echo 	^</tr^>
	)>>"!ReportPath!\default.htm"
	set /a count+=1
)
(
	echo ^</table^>
	echo ^</body^>
	echo ^</html^>
)>>"!ReportPath!\default.htm"

:growth
if /i "!growthswitch!" == "nogrowth" goto end
echo Generating growth report

echo Reading data
for /f %%a in ('dir /b "!DataDir!\*.tsv"^|find /v "~"^|find /v /i "growth"') do (
	set host=%%a
	set host=!host:~0,-4!
	set linedate=
	set count=0
	set curmonth=0
	set tmpvar=
	for /f "usebackq tokens=1* delims=	" %%b in ("!DataDir!\%%a") do (
		set linedate=%%b
		set linemonth=!linedate:~6!!linedate:~3,2!
		if !count! == 0 (
			>"!DataDir!\~!host!.tsv" echo %%b	%%c
		) else (
			if !linemonth! GTR !curmonth! (
				set curmonth=!linemonth!
				if not "!tmpvar!" == "" >>"!DataDir!\~!host!.tsv" echo !tmpvar!

			)
		set tmpvar=!linedate:~3!	%%c
		)
		set /a count+=1
	)
)
echo Calculating current growth
for /f %%a in ('dir /b "!DataDir!\~*.tsv"') do (
	set host=%%a
	set host=!host:~1,-4!
	set linedate=
	set count=0
	set curmonth=0
	set tmpvar=
	for /f "usebackq tokens=* delims=	" %%b in ("!DataDir!\%%a") do (
		if !count! == 0 (
			call :addcols %%b
		) else (
			call :docalc %%b
		)
		set /a count+=1
	)
	echo Disk	Disk Size	Months	Average Growth	Average Growth %%	Average %%Change in Growth Status>"!DataDir!\stats-!host!.txt"
)
echo Calculating projected growth
for /f %%a in ('dir /b "!DataDir!\Growth-*.tsv"') do (
	for /f %%c in ('type "!DataDir!\%%a"^|find /c "/"') do if %%c GTR 1 (
		set datafile=%%a
		set growthmax=0
		set growthmin=0
		for /f "tokens=2 delims=-" %%b in ("%%a") do set host=%%b
		if not "!lastcheck!" == "!host!" echo Creating graphs for !host!
		set lastcheck=!host!
		for %%b in (linecount,yrange,count,fccount,disksize,grtotal,gravg,grpcntlast,grpcnt,grtotalpt,grptchange,grptchngtotal,grpcntavg,grptchngavg) do set %%b=0
		set enddate=
		set highgrowth=no
		set status=limegreen
		for /f "tokens=* delims=	" %%b in ('type "!DataDir!\%%a"^|findstr /i "Date"') do call :getcols %%b
		set linecount=0
		for /f "usebackq tokens=1 delims=	" %%b in ("!DataDir!\%%a") do (
			set /a linecount+=1
			if !linecount! == 2 (
				set startdate=%%b
				if "!startdate:~0,2!" == "01" (
					set /a tmpstart=!startdate:~3!-1
					set tmpstart=!tmpstart!12
				) else (
					set tmpstart=!startdate:~3!!startdate:~0,2!
					set /a tmpstart=!tmpstart! - 1
				)
				set startdate=!tmpstart:~4!/!tmpstart:~0,4!
			)
			set enddate=%%b
			if "!enddate:~0,2!" == "12" (
				set /a fcstart=!enddate:~3! + 1
				set fcstart=!fcstart!01
			) else (
				set /a fcstart=!enddate:~3!!enddate:~0,2! + 1
			)
		)
		for /f "usebackq skip=1 tokens=2,4 delims=	" %%b in ("!DataDir!\%%a") do set dskused=%%c&if %%b GTR !disksize! set disksize=%%b
		for /f "usebackq skip=2 tokens=5 delims=	" %%b in ("!DataDir!\%%a") do (
			set /a fccount+=1
			if not "%%b" == " " (
				for /f %%c in ('clc !grtotal!+%%b') do set grtotal=%%c
				for /f %%c in ('clc !grtotal!/!fccount!') do set gravg=%%c
				if count GTR 1 set grpcntlast=!grpcnt!
				for /f %%c in ('clc %%b/!disksize!*100') do set grpcnt=%%c&for /f %%d in ('clc !grtotalpt!+%%c') do set grtotalpt=%%d
				for /f %%c in ('clc !grpcnt!-!grpcntlast!') do set grptchange=%%c&for /f %%d in ('clc !grptchngtotal!+%%c') do set grptchngtotal=%%d
				for /f %%c in ('clc !grtotalpt!/!fccount!') do set grpcntavg=%%c
				if !fccount! GTR 1 (
					for /f %%c in ('clc !grptchngtotal!/^(!fccount!-1^)') do set grptchngavg=%%c
				) else set grptchngavg=!grptchngtotal!
			)
		)
		set /a reportmonths=!fccount!+1
		set stats=!diskname!:	!disksize!	!reportmonths!	!gravg:~0,-4!	!grpcntavg:~0,-3!	!grptchngavg:~0,-3!
		for /f %%c in ('clc !grptchngavg!+!grpcntavg!') do set curgrowth=%%c||set curgrowth=0
		set fcdate=!fcstart!
		for /l %%b in (1,1,!growthmonths!) do (
			for /f %%c in ('clc !disksize!*!curgrowth!/100') do set dskgrowth=%%c
			for /f %%c in ('clc !dskused!+!dskgrowth!') do for /f "tokens=1 delims=." %%d in ("%%c") do if %%d LSS 0 (set dskused=0) else set dskused=%%c
			for /f %%c in ('clc !disksize!-!dskused!') do set dskfree=%%c
			echo !fcdate:~4!/!fcdate:~0,4!	!disksize!	 	 	 	!dskfree!	!dskused!	!dskgrowth! >>"!DataDir!\%%a"
			if not "%%b" == "!growthmonths!" (
				if "!fcdate:~4!" == "12" (
					set /a fcdate=!fcdate:~0,4! + 1
					set fcdate=!fcdate!01
				) else set /a fcdate+=1
				for /f %%c in ('clc !curgrowth!+!grptchngavg!') do set curgrowth=%%c
			)
		)
		for /f "tokens=1 delims=." %%c in ("!dskused!") do if %%c GTR !disksize! set status=red
		echo !stats!	!status!>>"!DataDir!\stats-!host!.txt"
		for /f "useback skip=1 tokens=2,5,7,8 delims=	" %%b in ("!DataDir!\%%a") do (
			if not "%%b" == "" if not "%%b" == " " for /f "tokens=1 delims=." %%f in ("%%b") do if %%f GTR !yrange! set yrange=%%f
			if not "%%d" == "" if not "%%d" == " " for /f "tokens=1 delims=." %%f in ("%%d") do if %%f GTR !yrange! set yrange=%%f
			if "%%c" == " " (
				for /f "tokens=1 delims=." %%f in ("%%e") do (
					if %%f GTR 999 set highgrowth=yes
					if %%f LSS -999 set highgrowth=yes
					if %%f GTR !growthmax! set growthmax=%%f
					if %%f LSS !growthmin! set growthmin=%%f
				)
			) else if not "%%c" == "" (
				for /f "tokens=1 delims=." %%f in ("%%c") do (
					if %%f GTR 999 set highgrowth=yes
					if %%f LSS -999 set highgrowth=yes
					if %%f GTR !growthmax! set growthmax=%%f
					if %%f LSS !growthmin! set growthmin=%%f
				)
			)
		)
		set /a yrange=!yrange! + ^( !yrange! / 20 ^)
		(
			echo #proc page
			echo #if @DEVICE in png,gif
			echo scale: 1.0
			echo croprel: 0.1 0.3 0.3 0.1
			echo backgroundcolor: white
			echo #endif
			echo.
			echo #proc getdata
			echo 	file: !DataDir!\!datafile!
			echo 	delimit: tab
			echo 	fieldnames: !fieldnames!
			echo.
			echo #proc areadef
			echo 	title: Disk Growth - !diskname!: Drive on !host!
			echo 	titledetails: size=14  align=C adjust=0,0.2
			echo 	rectangle: 1 1 7 4
			echo 	areacolor: gray^(0.7^)
			echo 	frame: width=1.0
			echo 	xscaletype: date mm/yyyy
			echo 	xrange: 15/!startdate! 15/!fcdate:~4!/!fcdate:~0,4!
			echo 	yrange: 0 !yrange!
			echo.
			echo #proc xaxis
			echo 	stubs: inc 1 month
			echo 	tics: none
			echo 	stubformat: Mmm
			echo.
			echo #proc yaxis
			echo 	label: GigaBytes
			echo 	stubmult: 0.001
			echo 	stubs: inc
			echo 	grid: color=gray^(0.8^)
			echo.
			echo #proc bars
			echo 	locfield: Date
			echo 	lenfield: !diskname!-Total
			echo 	color: green
			echo 	barwidth: 0.18
			echo 	legendlabel: !diskname!-Total
			echo #proc bars
			echo 	locfield: Date
			echo 	lenfield: !diskname!-Used
			echo 	color: brightblue
			echo 	barwidth: 0.18
			echo 	legendlabel: !diskname!-Used
			echo #proc bars
			echo 	locfield: Date
			echo 	lenfield: Projected-Used
			echo 	color: powderblue2
			echo 	barwidth: 0.18
			echo 	legendlabel: !diskname!-Used ^(forecast^)
			echo.
			echo #proc areadef
			echo 	rectangle: 1 1 7 4
			echo 	xscaletype: date mm/yyyy
			echo 	xrange: 15/!startdate! 15/!fcdate:~4!/!fcdate:~0,4!
			if "!highgrowth!" == "no" (
				echo 	yautorange datafield=!diskname!-Growth,Projected-Growth nearest=10 margin=20
			) else (
				echo 	yautorange datafield=!diskname!-Growth,Projected-Growth nearest=1000 margin=20
			)
			echo.
			echo #proc yaxis
			if "!highgrowth!" == "no" (
				echo 	label: Growth ^( MB / Month ^)
			) else (
				echo 	label: Growth ^( GB / Month ^)
				echo 	stubmult: 0.001
			)
			echo 	labeldistance: -0.55
			echo 	location: max
			echo 	stubs: inc
			echo 	stubdetails: adjust=0.25 align=L
			echo 	ticlen: 0 0.05
			echo.
			echo #proc lineplot
			echo 	xfield: Date
			echo 	yfield: !diskname!-Growth
			echo 	pointsymbol: fillcolor=red shape=triangle
			echo 	linedetails: color=yellow width=1.5
			echo 	legendlabel: !diskname!-Growth
			echo #proc lineplot
			echo 	xfield: Date
			echo 	yfield: Projected-Growth
			echo 	pointsymbol: fillcolor=blue shape=triangle
			echo 	linedetails: color=dullyellow width=1.5
			echo 	legendlabel: !diskname!-Growth ^(forecast^)
			echo.
			echo #proc legend
			echo 	format: singleline
			echo 	location: min+0.2 min-0.4
			echo 	outlinecolors: yes
			echo 	sep 0.5
			echo 	seglen: 0.2
		)>"!DataDir!\temp.gra"
		"!plpath!pl" "!DataDir!\temp.gra" -gif -o "!ReportPath!\images\Growth-!host!-!diskname!.gif"
	)
)
del "!DataDir!\temp.gra"

echo Building growth report
(
	echo ^<html^>
	echo ^<head^>
	echo 	^<title^>Disk Growth !CustomTitle!^</title^>
	echo 	^<link href="!CSSFile!" rel="stylesheet" type="text/css"^>
	echo ^</head^>
	echo ^<body^>
	echo ^<table width="100%%"^>
	echo 	^<tr^>
	echo 		^<td width="10"^>^</td^>
	echo 		^<td align="left"^>Disk Growth !CustomTitle!^</td^>
	echo 		^<td align="right" valign="top" width="30%%"^>Last updated at %time:~0,-6% on %date%^</td^>
	echo 	^</tr^>
	echo ^</table^>
	echo ^<br^>
	echo ^<center^>

	echo ^<table width="680"^>
	echo 	^<tr^>
	echo 		^<td align="center"^>^<a href="growth-all.html"^>View All Graphs^</a^>^</td^>
	echo 	^</tr^>
	echo ^</table^>
)>"!ReportPath!\growth.html"
for /f %%a in ('dir /b "!DataDir!\stats-*.txt"') do (
	for /f %%c in ('type "!DataDir!\%%a"^|find /c ":"') do if %%c GTR 1 (
		for /f "tokens=2 delims=-." %%b in ("%%a") do set host=%%b
		for /f %%b in ('type "!DataDir!\%%a"^|find /c ":"') do set /a numdisks=%%b + 1
		(
			echo ^<table width="680"^>
			echo 	^<tr^>
			echo 		^<td width="80" align="center" rowspan="!numdisks!"^>!host!^<br^>^<a href="growth-!host!.html"^>View Graphs^</a^>^</td^>
			echo 		^<td^>Disk^</td^>
			echo 		^<td^>Disk Size ^(MB^)^</td^>
			echo 		^<td^>Months^</td^>
			echo 		^<td^>Average Growth ^(MB^)^</td^>
			echo 		^<td^>Average Growth ^(%%^) ^</td^>
			echo 		^<td^>Avg Change ^(%%^) ^</td^>
			echo 		^<td^>Status^</td^>
			echo 	^</tr^>
		)>>"!ReportPath!\growth.html"
		for /f "usebackq skip=1 tokens=1-7 delims=	" %%b in ("!DataDir!\%%a") do (
			(
				echo 	^<tr^>
				echo 		^<td align="center"^>%%b^</td^>
				echo 		^<td align="right"^>%%c^</td^>
				echo 		^<td align="center"^>%%d^</td^>
				echo 		^<td align="right"^>%%e^</td^>
				echo 		^<td align="right"^>%%f^</td^>
				echo 		^<td align="right"^>%%g^</td^>
				echo 		^<td STYLE="background: %%h; color=white;"^>^ ^</td^>
				echo 	^</tr^>
			)>>"!ReportPath!\growth.html"
		)
		echo ^</table^>>>"!ReportPath!\growth.html"
	)
)
(
	echo ^<table width="680"^>
	echo 	^<tr^>
	echo 		^<td align="center"^>^<a href="growth-all.html"^>View All Graphs^</a^>^</td^>
	echo 	^</tr^>
	echo ^</table^>
	echo ^</body^>
	echo ^</html^>
)>>"!ReportPath!\growth.html"
echo Building graphical reports
(
	echo ^<html^>
	echo ^<head^>
	echo 	^<title^>Disk Growth !CustomTitle! - All Servers^</title^>
	echo 	^<link href="!CSSFile!" rel="stylesheet" type="text/css"^>
	echo ^</head^>
	echo ^<body^>
	echo ^<table width="100%%"^>
	echo 	^<tr^>
	echo 		^<td width="10"^>
	echo 		^<td align="left"^>Disk Growth !CustomTitle! - All Servers^</td^>
	echo 		^<td align="right" valign="top" width="30%%"^>Last updated at %time:~0,-6% on %date%^</td^>
	echo 	^</tr^>
	echo ^</table^>
	echo ^<center^>
	echo ^<table^>
)>"!ReportPath!\growth-all.html"
set count=0
for /f %%a in ('dir /b "!ReportPath!\images\Growth-*.gif"') do (
	if not !count! == 0 (
		(
			echo 	^<tr height="10"^>
			echo 		^<td colspan="3" align="center"^>^ ^</td^>
			echo 	^</tr^>
		)>>"!ReportPath!\growth-all.html"
	)
	(
		echo 	^<tr^>
		echo 		^<td colspan="2"^>^</td^>
		echo 		^<td^>
		echo 	^</tr^>
		echo 	^<tr^>
		echo 		^<td^>^</td^>
		echo 		^<td align="center"^>^<img src="images/%%a"^>^</td^>
		echo 		^<td^>^</td^>
		echo 	^</tr^>
		echo 	^<tr^>
		echo 		^<td^>^</td^>
		echo 		^<td^>^ ^</td^>
		echo 		^<td^>^</td^>
		echo 	^</tr^>
	)>>"!ReportPath!\growth-all.html"
	set /a count+=1
)
(
	echo ^</table^>
	echo ^</body^>
	echo ^</html^>
)>>"!ReportPath!\growth-all.html"

for /f %%a in (!curdir!\servers.txt) do (
	for /f %%b in ('dir /b "!ReportPath!\images\*.gif"^|find /c "Growth-%%a"') do if %%b GTR 0 (
		(
			echo ^<html^>
			echo ^<head^>
			echo 	^<title^>Disk Growth !CustomTitle! - %%a^</title^>
			echo 	^<link href="!CSSFile!" rel="stylesheet" type="text/css"^>
			echo ^</head^>
			echo ^<body^>
			echo ^<table width="100%%"^>
			echo 	^<tr^>
			echo 		^<td width="10"^>
			echo 		^<td align="left"^>Disk Growth !CustomTitle! - %%a^</td^>
			echo 		^<td align="right" valign="top" width="30%%"^>Last updated at %time:~0,-6% on %date%^</td^>
			echo 	^</tr^>
			echo ^</table^>
			echo ^<center^>
			echo ^<table^>
		)>"!ReportPath!\growth-%%a.html"
		set count=0
		for /f %%c in ('dir /b "!ReportPath!\images\Growth-%%a*.gif"') do (
			if not !count! == 0 (
				(
					echo 	^<tr height="10"^>
					echo 		^<td colspan="3"^>^ ^</td^>
					echo 	^</tr^>
				)>>"!ReportPath!\growth-%%a.html"
			)
			(
				echo 	^<tr^>
				echo 		^<td colspan="2"^>^</td^>
				echo 		^<td^>
				echo 	^</tr^>
				echo 	^<tr^>
				echo 		^<td^>^</td^>
				echo 		^<td align="center"^>^<img src="images/%%c"^>^</td^>
				echo 		^<td^>^</td^>
				echo 	^</tr^>
				echo 	^<tr^>
				echo 		^<td^>^</td^>
				echo 		^<td^>^ ^</td^>
				echo 		^<td^>^</td^>
				echo 	^</tr^>
			)>>"!ReportPath!\growth-%%a.html"
			set /a count+=1
		)
		(
			echo ^</table^>
			echo ^</body^>
			echo ^</html^>
		)>>"!ReportPath!\growth-%%a.html
	)
)

:logrotate
echo Pruning data
set yyyy=!today:~6!
set mm=!today:~3,2!
set dd=!today:~0,2!
set /a cutoff=!yyyy!-1
set /a cutoff=!cutoff!!mm!!dd!

if not exist "!DataDir!" echo Data directory !DataDir! does not exist&goto exit

echo Removing entries before !cutoff:~6!/!cutoff:~4,2!/!cutoff:~0,4!

for /f %%a in ('dir /b "!DataDir!\*.tsv"^|find /v "~"^|find /v "Growth-"') do (
	for /f "usebackq tokens=1* delims=	" %%b in ("!DataDir!\%%a") do (
		if "%%b" == "Date" (
			echo %%b	%%c  >"!DataDir!\tmp.txt"
		) else (
			for /f "tokens=1-3 delims=/" %%d in ("%%b") do set entrydate=%%f%%e%%d
			if not "!entrydate!" LEQ "!cutoff!" (echo %%b	%%c  >>"!DataDir!\tmp.txt") else echo %%b discarded from %%a
		)
	)
	if exist "!DataDir!\tmp.txt" (
		if exist "!DataDir!\%%a.old" del "!DataDir!\%%a.old"
		ren "!DataDir!\%%a" %%a.old
		ren "!DataDir!\tmp.txt" %%a
	)
)

del /q "!DataDir!\*.old">nul

goto end

:getlines

if "%1" == "" goto:eof
set fieldnames=
set lines=
set colcount=0
:loop1
set /a colcount+=1
set fieldnames=!fieldnames!%1 
set tmpvar=%1
if "!tmpvar:~3!" == "Free" set lines=!lines!%1,
shift
if not "%1" == "" goto loop1

goto:eof

:getrange

for /f "usebackq tokens=1-%1 delims=	" %%b in (%2) do (
	set /a daycount=!daycount!+1
	if !daycount! == 2 set startdate=%%b
	set linedate=%%b
)

goto:eof

:getcols

if "%1" == "" goto:eof
set fieldnames=
set lines=
set colcount=0
:loop1
set /a colcount+=1
set fieldnames=!fieldnames!%1 
set tmpvar=%1
if "!tmpvar:~2!" == "Free" set diskname=!tmpvar:~0,1!

shift
if not "%1" == "" goto loop1

goto:eof

:addcols

set tmpvar=%1
shift
set diskcount=1
:loop2
set curdisk=%1
set curdisk=!curdisk:~0,1!
echo !tmpvar!	%1	%2	%3	!curdisk!-Growth	Projected-Free	Projected-Used	Projected-Growth>"!DataDir!\Growth-!host!-!diskcount!.tsv"
shift
shift
shift
shift
set /a diskcount+=1

if not "%1" == "" goto loop2

goto:eof

:docalc

set tmpvar=%1
shift
set curdisk=0
set diskcount=1
:loop3

set /a lastmonth=!count!-1&set lastmonth=month!lastmonth!
set /a curdisk+=1
for /l %%n in (1,1,12) do if !count! == %%n set month%%ndisk!curdisk!=%3
set lastval=!lastmonth!disk!curdisk!
set /a growth=%3-!lastval!

if not !count! == 1 (
	echo !tmpvar!	%1	%2	%3	!growth!	>>"!DataDir!\Growth-!host!-!diskcount!.tsv"
) else (
	echo !tmpvar!	%1	%2	%3	>>"!DataDir!\Growth-!host!-!diskcount!.tsv"
)

shift
shift
shift
shift
set /a diskcount+=1

if not "%1" == "" goto loop3

goto:eof

:starterror
echo Incorrect switch! command line arguments are one of:
echo 	-graphonly 	- Only generates the daily trend graph, no data collection
echo 	-growthonly 	- Only generates the growth table and graphs, no data collection
echo 	-nogrowth 	- Does not generate the growth table and graphs

:end

del /q "!DataDir!\*.srv"
del /q "!DataDir!\~*.tsv"
del /q "!DataDir!\Growth-*.tsv"
del /q "!DataDir!\stats-*.txt"
echo Done.
endlocal