forked from fsprojects/fantomas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
performance.fs
76 lines (67 loc) · 2.42 KB
/
performance.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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/// The program is from http://codereview.stackexchange.com/q/20068
module Performance
open System
open System.Drawing
open System.IO
open System.Text
open System.Text.RegularExpressions
let rec getAllFiles baseDir =
seq {
yield! Directory.EnumerateFiles(baseDir)
for dir in Directory.EnumerateDirectories(baseDir) do
yield! getAllFiles dir
}
let dateTakenFromExif file =
let r = new Regex(":")
use fs = new FileStream(file, FileMode.Open, FileAccess.Read)
use myImage = Image.FromStream(fs, false, false)
let propItem = myImage.GetPropertyItem(36867)
let dateTaken = r.Replace(Encoding.UTF8.GetString(propItem.Value), "-", 2)
DateTime.Parse(dateTaken)
let getDateTaken file =
try
dateTakenFromExif file
with :? Exception -> File.GetLastWriteTime(file) //Use the last write time in the event that the file was moved/copied
let addToFile file n =
match n with
| 2 ->
let fileDir = Path.GetDirectoryName(file)
let nextFile =
Path.GetFileNameWithoutExtension(file) + "-" + n.ToString()
+ Path.GetExtension(file)
Path.Combine(fileDir, nextFile)
| _ ->
let prev = n - 1
file.Replace("-" + prev.ToString(), "-" + n.ToString())
let getNewFilename newFilePath =
let rec incrementFilename file n =
let filenameIncremented = addToFile file n
match File.Exists(filenameIncremented) with
| false -> filenameIncremented
| true -> incrementFilename filenameIncremented (n + 1)
match File.Exists(newFilePath) with
| false -> newFilePath
| true -> incrementFilename newFilePath 2
let move destinationRoot files =
let moveHelper file =
let dateTaken = getDateTaken file
let finalPath =
Path.Combine
(destinationRoot, dateTaken.Year.ToString(),
dateTaken.ToString("yyyy-MM-dd"))
if not (Directory.Exists(finalPath)) then
Directory.CreateDirectory(finalPath) |> ignore
let newFile =
getNewFilename (Path.Combine(finalPath, Path.GetFileName(file)))
try
File.Copy(file, newFile)
with :? Exception as e ->
failwith (sprintf "error renaming %s to %s\n%s" file newFile e.Message)
files |> Seq.iter moveHelper
let moveFrom source =
getAllFiles source
|> Seq.filter (fun f -> Path.GetExtension(f).ToLower() <> ".db") //exlcude the thumbs.db files
|> move """C:\_EXTERNAL_DRIVE\_Camera"""
printfn "Done"
moveFrom """C:\Users\Mike\Pictures\To Network"""
moveFrom """C:\_EXTERNAL_DRIVE\Camera"""