forked from ploeh/picture-archivist
/
Archive.fs
28 lines (22 loc) · 965 Bytes
/
Archive.fs
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
namespace Ploeh.Samples
open System
open System.IO
type PhotoFile = { File : FileInfo; TakenOn : DateTime }
type Move = { Source : FileInfo; Destination : FileInfo }
module Archive =
let moveTo destination t =
let dirNameOf (dt : DateTime) = sprintf "%d-%02d" dt.Year dt.Month
let groupByDir pf m =
let key = dirNameOf pf.TakenOn
let dir = Map.tryFind key m |> Option.defaultValue []
Map.add key (pf.File :: dir) m
let addDir name files dirs =
Tree.node name (List.map Leaf files) :: dirs
let m = Tree.foldBack groupByDir t Map.empty
Map.foldBack addDir m [] |> Tree.node destination
let calculateMoves =
let replaceDirectory (f : FileInfo) d =
FileInfo (Path.Combine (d, f.Name))
let leaf x path = { Source = x; Destination = replaceDirectory x path }
let node x path = Path.Combine (path, x)
Tree.scan node leaf ""