Xah Talk Show 2026-01-08 Ep740 Learning Fsharp. Write a Find Replace Script. Part 3.

Video Summary (Generated by AI, Edited by Human.)

fsharp, date time

Fsharp, copy file

// copy file example

open System.IO

File.Copy("c:/Users/xah/web/xahlee_info/talk_show/xah_talk_show_ep740.html", "c:/Users/xah/web/xahlee_info/talk_show/xah_talk_show_ep740.html~x~")
// Does not overwrite if destination exists

// Or, to allow overwriting an existing destination file:
// File.Copy("source.txt", "destination.txt", overwrite = true)

fsharp, count occurance

// 2026-01-08
// this is a example of finding count of occurance
// does not work

open System   // for StringComparison if needed

let xinputText = "Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do: once or twice she had peeped into the book her sister was reading, but it had no pictures or conversations in it, “and what is the use of a book,” thought Alice “without pictures or conversation?”."

let xfindStr = "Alice"

let count =
    let rec floop zindex xcount =
        let pos = xinputText.IndexOf(xfindStr, zindex)
        if pos = -1 then xcount
        else floop (pos + xfindStr.Length) (xcount + 1)
    floop 0 0

let didReplaceHappen = count > 0
let result = xinputText.Replace(xfindStr, replaceWith)

// printfn "didReplaceHappen is %s" didReplaceHappen
// 2026-01-08
// this is a example of finding count of occurance
// does not work

open System.Text.RegularExpressions
// open System.Text.Regex

// open System   // for StringComparison if needed

let xinputText = "Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do: once or twice she had peeped into the book her sister was reading, but it had no pictures or conversations in it, “and what is the use of a book,” thought Alice “without pictures or conversation?”."

let xfindStr = "Alice"
let xreplaceStr = "Queen"

let regex = Regex(Regex.Escape(xfindStr))  // escape if needed
// let matchResult = regex.Replace(xinputText, xreplaceStr, count = Int32.MaxValue)  // the new string

// In .NET 8+, you can get the count directly:
let replaceResult = regex.Replace(xinputText, xreplaceStr, count = Int32.MaxValue, startingPosition = 0)
let didReplaceHappen = replaceResult.ResultCount > 0

Fsharp, find replace script

// find and replace, of all files in a dir
// 2026-01-03

open System.IO // for getting dir content
open System // for DateTime

let xinputpath = "c:/Users/xah/web/xahlee_info/talk_show/xxelisp/"
let xfilenameExtensionGlob = "A*.html"

let xfindString = "Saving"
let xreplaceString = "xSaving"

// s------------------------------

let xdatetime = DateTime.Now.ToString("yyyy-MM-dd_HHmmss")

// todo. make backup. with timestamp in the filename.
// Or, to allow overwriting an existing destination file:
// File.Copy("source.txt", "destination.txt", overwrite = true)

// All files recursively
let xfileList = Directory.EnumerateFiles(xinputpath, xfilenameExtensionGlob , SearchOption.AllDirectories)

// process each file
let fprocessFile (xfilePath: string) =
    let xcontent: string = File.ReadAllText(xfilePath)
    let xnewContent = xcontent.Replace(xfindString, xreplaceString)
    File.WriteAllText(xfilePath, xnewContent)
// todo. modify it so it only print changed file
// in fsharp, when you do xcontent.Replace(xfindString, xreplaceString), how to find if replace actually happened
    if (xcontent <> xnewContent) then
        printfn "changed %s" xfilePath
    else
        printfn "no change %s" xfilePath

// walk the dir list
xfileList |> Seq.iter fprocessFile

printfn "done"

Xah Talk Show fsharp learn