Skip to content

Calculating the size of a directory in F#

July 27, 2011

Alas there is no Win32 API for calculating the size of a directory, so the best way is to just calculate the length of each file within the directory:

open System.IO

let rec size(fileSystemInfo : FileSystemInfo) =
    match fileSystemInfo with
    | :? FileInfo as fileInfo -> fileInfo.Length
    | :? DirectoryInfo as dirInfo -> dirInfo.GetFileSystemInfos() 
                                     |> Seq.sumBy(fun finfo -> size(finfo))
    | _ -> int64 0

No comments yet

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: