find and replace text in all text files in a folder

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
way2000
Posts: 58
Joined: 15 Jul 2016, 08:57

find and replace text in all text files in a folder

19 Oct 2017, 14:22

i have a program that i want to use to replace text in text files on a folder, so far i have the interface design but there's no functions in it

Image

Code: Select all

#NoEnv
#SingleInstance Force
SetWorkingDir %A_ScriptDir%

Gui Add, Button, x394 y43 w80 h23, &Execute
Gui Add, Edit, x9 y44 w378 h152, Edit
Gui Add, Edit, x9 y17 w378 h19, Edit
Gui Add, ComboBox, x9 y203 w377, Replace With|Insert Before|Insert After|Insert Between|Delete|Insert Top|Insert Bottom
Gui Add, Edit, x9 y230 w378 h152, Edit
Gui Add, Button, x394 y71 w80 h23, &Stop
Gui Add, Progress, x10 y400 w461 h20 -Smooth, 33
Gui Add, StatusBar,, Status Bar
Gui Add, Button, x394 y15 w80 h23, &Browse...

Gui Show, w482 h448, Batch replace text in text files
Return

GuiEscape:
GuiClose:
    ExitApp
please help to finish this program
User avatar
Exaskryz
Posts: 2882
Joined: 17 Oct 2015, 20:28

Re: find and replace text in all text files in a folder

19 Oct 2017, 14:39

You'll need a glabel, notably on your Execute button and Browse buttons. Browser should trigger the FileSelectFolder command.

For Execute, you should run a Loop, Files on the directory chosen by Execute; and use FileRead to load up the file into AHK memory. RegExReplace() or a StrReplace() using the variable from FileRead as your haystack, using the first large Edit box's variable as a needle, and then the other large Edit box's variable as a replacement string. Which brings up one more point: You need gui-variables, explained in the glabel link and also in Gui, Submit command. Then you'd just need to save each file; FileAppend will not overwrite the file, so a FileDelete would need to be done first. (I personally like to make it FileAppend to a newly named file, like "myFile-2.txt" or what have you; then if I'm satisfied that the script seems to be working right, I'd FileMove.)
teadrinker
Posts: 4326
Joined: 29 Mar 2015, 09:41
Contact:

Re: find and replace text in all text files in a folder

19 Oct 2017, 16:25

Actually, finding and replacing text in plenty of files through AHK may not be the best solution, particularly with RegexMatch, since that may take quite a long time. I'd prefer using Powershell for this purpose, like this:

Code: Select all

SetBatchLines, -1
filePath := A_Desktop . "\test.txt"

FileDelete, % filePath

Loop  10000
   str .= "Hello, World! "

FileAppend, % str, % filePath
MsgBox, Created! Press OK to start replacing.

FindReplaceInFile(filePath, "World", "AutoHotkey")
Run, % filePath

FindReplaceInFile(filePath, what, with, wait := true)  {
   command := "powershell.exe ""(Get-Content " . filePath . ") "
                              . "-replace '"   . what . "', '" . with . "'"
                           . " | Set-Content " . filePath . """"
   if wait
      RunWait, % command,, Hide
   else  {
      Run, % command,, Hide, PID
      return PID
   }
}
RegEx is supported also.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Araphen, ArkuS, claudiosxj, Descolada, mikeyww, OrangeCat and 328 guests