My first F# program: a command-line program that takes a starting path, a file name pattern, and pattern to search for, iterates through the path and its subfolders loading all files with the given file name pattern and returning the names of those files that contain the pattern to search for.
// Learn more about F# at http://fsharp.net
open System.IO
[<EntryPoint>]
let main (args : string[]) =
let filesInFolder : string [] =
System.IO.Directory.GetFiles(
args.[0], args.[1],
SearchOption.AllDirectories)
let fileInfos : System.IO.FileInfo[] =
Array.map
(fun file -> new System.IO.FileInfo(file))
filesInFolder
let getStreamForFile (file : FileInfo) : StreamReader =
new StreamReader(file.OpenRead())
let getFileContents (fileStream : StreamReader) : string =
fileStream.ReadToEnd()
let fileContentsContainPattern (fileContents : string) : bool =
fileContents.Contains(args.[2])
let writeNameOfFilesContainingPattern(file : FileInfo, containsPattern : bool) =
match containsPattern with
| true -> System.Console.WriteLine(file.FullName)
Array.map
(fun fileInfo -> writeNameOfFilesContainingPattern(fileInfo,
fileContentsContainPattern(getFileContents(getStreamForFile(fileInfo)))))
fileInfos
0
No comments:
Post a Comment