I am trying to replace multiple strings in all files within a folder. The problem is that -replace operator does not take into account the exact words that I need to replace for example:
I need to replace strings:
- RUN → Run
- RUNMODAL → RunModal
Now when I run my script it replaces the RUN strings into Run which is good, but it replaces also RUNMODAL into RunMODAL and it does not take into account my second condition. Is there any way to specify that only exact matches should be taken into account or at least for each replace I'd specify the number of characters to be taken into consideration when replacing a particular string?
$AllFiles = Get-ChildItem $FilePath
foreach ($file in $AllFiles) {
(Get-Content $file.PSPath) | ForEach {
$_ -creplace 'RUN', 'Run' `
-creplace 'RUNMODAL', 'RunModal'
} | Set-Content $file.PSPath
}
Edit:
Maybe a better example would be:
- FIELD → field
- NEWFIELD → NewField
Even if I switch these to I would either get NEWfield or Newfield and I need NewField.