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

- https://youtu.be/s_UIDix68kg
- Xah Talk Show 2022-01-06 WolframLang Tutorial, Find/Replace String in a Dir
- Python: Find Replace Text in Directory 📜
- Python: Find Replace Regex in Dir
- Golang: Regex Find Replace Text in Directory 📜
- Perl: Find Replace Text in Directory 📜
- Emacs: Xah Find Replace (xah-find.el)
write a find replace script in WolframLang
things need to do:
- list dir and subdirectory files
- how to filter by file name extension
- copy file (for backup)
- get date in iso format (for backup)
- find replace string with regex
- open file for read
- write to file
(*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]]