Batch replacing a word in txt files (e.g. during tagging or lora creation)

Hello Civitans,
so, it happened one day that I was training a lora and had my pics nicely tagged but I realized that many of these files (ca. 150) contain an expression which would distort the final results. To finetune the database I had to replace this one word for another. And then I said: 'There must be a program for this. I don't want to do it one by one.' And well, there are some programs for it indeed but most of them are paid softwares or the demo version puts extra text in the txt files (ie. 'This txt file was edited by ABC software') which then again corrupts the database.
Then I have found a great and easy solution and I thought to share it with you guys, it might be useful for some of you folks too :-)
So here is a step-by-step guide which needs no programming skills :))
STEP 1
Create a new empty .txt file
STEP 2
Copy and paste this text into it, then hit save:
Get-ChildItem 'C:\Folder1\Folder2\*.txt' -Recurse | ForEach {
(Get-Content $_ | ForEach { $_ -replace 'originaltext', 'newtext' }) |
Set-Content $_
}
STEP 3
Place this file into the folder where the .txt files are what you want to change.
STEP 4
As you can see the script in the end of the second line has two texts in qoute marks:
'originaltext' and 'newtext'.
Change the 'originaltext' to anything what you want to change from and change 'newtext' to what you want change the text to.
So, if you want to change the word 'glasses' to 'eyeglasses', here it is what you want to have:
Get-ChildItem 'C:\Folder1\Folder2\*.txt' -Recurse | ForEach {
(Get-Content $_ | ForEach { $_ -replace 'glasses', 'eyeglasses' }) |
Set-Content $_
}
Once done, you also want to change the location. Left-click on the file, then choose Properties, there you will find a line called: 'Location'.
Copy that text, it should look like this:
C:\AI\loratraining\eyeglasses
Now you want to replace the location, please make sure that you leave the last part in the script of the address, which is '...\*.txt' untouched. Othervise it will not work!
So, now your script will look something like this:
Get-ChildItem 'C:\AI\loratraining\eyeglasses\*.txt' -Recurse | ForEach {
(Get-Content $_ | ForEach { $_ -replace 'glasses', 'eyeglasses' }) |
Set-Content $_
}
STEP 5
Change the extension of this .txt file to .ps1.
In windows you can do this simply by selecting the file, then left-clicking on it once. You overwrite the end part .txt to .ps1.
STEP 6
There is a program created by Microsoft, called PowerShell. A little description of it from Wiki:
'PowerShell is bundled with all currently supported Windows versions, and can also be installed on MacOS and Linux. Since Windows 10 build 14971, PowerShell replaced Command Prompt (cmd.exe) and became the default command shell for File Explorer.'
If you are not using windows, you want to install it first:
https://learn.microsoft.com/en-us/powershell/?view=powershell-7.4
Step 6 is running the file which you can do by right-clicking on your .ps1 file and choose 'Run with PowerShell'.
DONE