Skip to content

Instantly share code, notes, and snippets.

@xfsnowind
Last active August 29, 2015 14:27
Show Gist options
  • Save xfsnowind/5e3cc859009cbc9b77ad to your computer and use it in GitHub Desktop.
Save xfsnowind/5e3cc859009cbc9b77ad to your computer and use it in GitHub Desktop.
open System.IO
open System.Security.Cryptography
let rec getAllFiles dir =
seq { yield! Directory.EnumerateFiles(dir, "*.*")
for d in Directory.EnumerateDirectories(dir) do
yield! getAllFiles d }
let getFileSize (filePath: string) =
FileInfo(filePath).Length
let fileHash (filePath : string) =
MD5.Create().ComputeHash (File.ReadAllBytes filePath)
let greaterThanFileSize (filePath: string) (threshHold: int64) =
filePath |> getFileSize |> (<) threshHold
let filesMatch (files: seq<string>) =
if false = (Seq.isEmpty files) then
printfn "The duplicated file:"
files |> Seq.iter (printfn "%s")
let duplicatedFile (filePath: string) (limitedFileSize: int64) =
try
getAllFiles filePath
|> Seq.filter (fun filePath -> greaterThanFileSize filePath limitedFileSize) // filter the files are bigger than given size
|> Seq.groupBy (fun file -> (fileHash file)) // group files by its hash code
|> Seq.map snd // take only grouped files
|> Seq.filter (fun files -> (>) (Seq.length files) 1) // if duplicated
|> Seq.iter (fun value -> (filesMatch value)) // print it
with
| :? System.IO.DirectoryNotFoundException -> printfn "The given path is not valid!"
| ex -> printfn "Failure: %s" (ex.ToString())
[<EntryPoint>]
let main(args: array<string>) =
if args.Length < 2 then
printfn "The length of arguments should be two."
else
try
let limitedFileSize = int64 args.[0]
let filePath = args.[1]
duplicatedFile filePath limitedFileSize
with
| _ -> printfn "The first value must be a valid number."
0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment