Sal-laS
Sal-laS

Reputation: 11649

How to get size of a file

I want to get the size of the files in a directory, but i don't know how to do it. here is my code:

 let givenDirectory="c:/Windows"

 let listOfFilesInDirectory  (givenDirectory:string)=
     let  listOfFiles=Directory.GetFiles  givenDirectory |> List.ofSeq
     let  listOfNames = listOfFiles |> List.map Path.GetFileName
     listOfNames


listOfFilesInDirectory givenDirectory

at first i pass a path to the fuction, and i get the full path of every single file in that directory, but i wonder how can i get te size of these files?

Upvotes: 0

Views: 718

Answers (2)

Helge Rene Urholm
Helge Rene Urholm

Reputation: 1188

RTFM?

You are using the System.IO namespace. Your reasons for not looking at documentation and then finding the class FileInfo(String) is ...? https://msdn.microsoft.com/en-us/library/system.io.fileinfo(v=vs.110).aspx

open System.IO

let fileInfo = FileInfo(@"c:\my\file\somewhere.txt") // you may use "c:\\my\\file etc for escape instead of using @ ahead of strings...
let fileLen =  fileInfo.Length

This better not be part of some homework\assignment! ;-)

Upvotes: 0

Tomas Petricek
Tomas Petricek

Reputation: 243061

You can use the FileInfo class:

let listOfFilesInDirectory  (givenDirectory:string)=
    let  listOfFiles=Directory.GetFiles  givenDirectory |> List.ofSeq
    listOfFiles |> List.map (fun fn -> 
        Path.GetFileName(fn), FileInfo(fn).Length)

Given a file name fn, you can create FileInfo(fn) and then get the file length using the Length property. The above returns a list with file names together with their sizes.

Upvotes: 2

Related Questions