Autohotkey: How can I use loop function to readfiles TXT format and replace content inside?

Viewed 10

I am quite new with AHK, I would like to query how I can use AutoHotKey loop to read many TXT files in a folder, look for content string/Regex and replace with other string/Regex, so far i got this (but i cannot apply it to a loop, I have tried to look into the official documentation but still cannot make it works) I have tried to change to M3 to select multiple files, still does not works:

FileSelectFile, SelectedFile, 3,%A_WorkingDir%, Open a file, , ,(*.txt)
    
fileread, text, % SelectedFile

text := RegExReplace(text,"(?<=\<target\>).*?(?=\<\/target\>)"," ",all) ;this will look content between tags <target> and replace with empty content 

filedelete, % SelectedFile

fileappend, %text%, % SelectedFile

Thank you in advance!

1 Answers

Look at AHK help for Loop (files & folders):

https://www.autohotkey.com/docs/commands/LoopFile.htm

and then try:

Loop, Files, *.txt
{
  fileread, text, % A_LoopFileName
  text := RegExReplace(text,"(?<=\<target\>).*?(?=\<\/target\>)"," ",all) ;this will look content between tags <target> and replace with empty content
  filedelete, % A_LoopFileName
  fileappend, %text%, % A_LoopFileName
}

You can of course be more express with the file full path (and even recurse into sub-folders), and make sure to look at all the built in Special Variables that are available inside a File-Loop.

Hth,

Related