Xah Talk Show 2022-01-06 WolframLang Tutorial, Find/Replace String in a Dir

Xah Talk Show 2022-01-06 WolframLang Tutorial, Find/Replace String in a Dir

write a find/replace script in WolframLang

things need to do:

(*find and replace strings in a dir. Version 2022-01-06*)

inputDir = "c:/Users/xah/xx/";
makeBackUp = True;
dirDepth = Infinity;
fileNameExtension = "*html";

findReplacePairs = {"initial" -> "XYZ", "commands" -> "Xcommands",
   "somethingNotThere" -> "Xcommands"};

processFile =
  Function[{filePath},
   Module[{content = ReadString[filePath]},
    If[makeBackUp,
     CopyFile[filePath, StringJoin[filePath, "~", nowDate, "~"],
      OverwriteTarget -> True]];
    content = StringReplace[content, findReplacePairs];
    WriteString[filePath, content]]];

Block[{nowDate = DateString[Now, "ISODate"]},
 processFile /@ FileNames[fileNameExtension, inputDir, dirDepth]]

xah_talk_show_2022-01-06.txt