Xah Talk Show 2022-01-06 WolframLang Tutorial, Find/Replace String in a Dir
- Python: Script to Find Replace Plain Text (No Regex) in a Directory
- Python: Script to Find Replace Multi-Pairs Regex in a Directory
- Golang: Script to Find Replace Multi-Pairs of Regex in a Directory
- Perl: Find Replace String Pairs 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]]