The routine is a working program that helps optimize disk usage by choosing which files to select for burning.
Suppose you have a bunch of images that you want to burn to a disc with given capacity, for example a 4.3 Gb DVD.
The total size of your files however exceeds the given capacity. So you need to choose which files to burn so that the wasted space is minimized.
The program has a LOT of room for improvement. It can't handle more than ~20 items, so to work around this limitation I manually group items in folders. This could and should be done by the program automatically.
Also:
* There's a bunch of potentially unnecessary variables that should be eliminated
* There's a significant delay upon first run, possibly due to Manipulate, but I can't figure it out
For now I'd welcome suggestions on how to optimize/shorten the code, so that it is more readable and more robust.
I'd like to postpone the implementation of new functionality until after we optimize the existing code.
I'm sure by the end this code would probably look completely different and we all can learn a lot in the process.
Looking forward to all comments and suggestions!
Here's my original v 1.0 of the code:
Manipulate[Column[{folder, capacity, result}],
{folder, Directory[], FileNameSetter[#, "Directory"] &},
{{capacity, 703.}, {703. -> "CD 700Mb", 4479. -> "DVD 4.7Gb",8106. -> "DVD DL 8.5Gb", 23098. -> "BluRay 25Gb"}},
{{result, Null}, Button["Compute", result = (
SetDirectory[folder];
files =With[{fn =If[(FileNames[] // Length) <= 16, FileNames[],FileNames[][[1 ;; 16]]]},
Thread[List[fn, (FileByteCount[fn] + dirByteCount[fn])/1024./1024.]]];
list = Sort[With[{p = Subsets[files, {2, Infinity}]},Thread[List[(#[[All, 2]] // Total ) & /@ p, p]]], #1[[1]] > #2[[1]] &];
plot = ListPlot[list[[All, 1]]];
list = Select[list, #[[1]] <= capacity &][[1]];
Grid[{{GraphicsRow[{Show[plot, Plot[capacity, {x, 0, 30000}]],PieChart3D[{list[[1]], capacity - list[[1]]},ChartLegends -> {"Used", "Wasted"}]}, ImageSize -> 800]},
{With[{d = list[[2, All, 1]], s = list[[2, All, 2]],t = list[[2, All, 2]] // Total,pf := ToString[PaddedForm[#, {10, 2}]] &},
Grid[Join[Transpose[{Range[d // Length], d, pf /@ s,pf /@ (100 s/capacity)}], {{, , pf[t],pf[(100 t/capacity)] ~~ "%"}}], Frame -> All,
Alignment -> {Left, Right, Right}]]}}]
)] &},
SynchronousInitialization -> False, SynchronousUpdating -> False,
Initialization :> (dirByteCount[dir_] := (FileByteCount /@ FileNames["*", {dir}, Infinity] //Total);
SetAttributes[{dirByteCount, FileByteCount}, Listable];)
]
Here's a screenshot: