$root = "C:\Users\dionmoustos\Projects\pokefirered\" Get-ChildItem -Path $root -Recurse -File -Filter "scripts.inc" | ForEach-Object { Write-Host "Processing: $($_.FullName)" # ---------------------------- # Read entire file as one string # ---------------------------- $content = Get-Content -Path $_.FullName -Raw $new = $content # ---------------------------- # Regex pattern (PowerShell-safe) # Your pattern: # [a-zA-Z_0-9]+::(\n\t[a-zA-Z_0-9 ,]+)+ # ---------------------------- $pattern = '[a-zA-Z_0-9]+::(\r?\n\t[a-zA-Z_0-9 ,]+)+' # ---------------------------- # Extract matches as array of multiline strings # ---------------------------- $matches = [regex]::Matches($content, $pattern) $array = @() foreach ($m in $matches) { $array += $m.Value } # ---------------------------- # Do replacements on original string # (example transformation per item) # ---------------------------- foreach ($item in $array) { if ($item -match '^\s*([A-Za-z0-9_]+)::\s*\r?\n\s*trainerbattle_single\s+([A-Za-z0-9_]+),\s*([A-Za-z0-9_]+),\s*([A-Za-z0-9_]+)\r?\n\s*msgbox\s+([A-Za-z0-9_]+),\s*([A-Za-z0-9_]+)\r?\n\s*end') { $eventScript = $matches[1] $trainer = $matches[2] $introText = $matches[3] $defeatText = $matches[4] $postbattle = $matches[5] $messagebox = $matches[6] $replacement = "$eventScript:: trainerbattle_single $trainer, $introText, $defeatText specialvar VAR_RESULT, ShouldTryRematchBattle goto_if_eq VAR_RESULT, TRUE, $eventScript`Rematch msgbox $postbattle, $messagebox end $($eventScript)Rematch:: trainerbattle_rematch $trainer, $introText, $defeatText msgbox $postbattle, $messagebox end" $content = $content -replace [regex]::Escape($item), $replacement } elseif ($item -match '^\s*([A-Za-z0-9_]+)::\s*\r?\n\s*trainerbattle_double\s+([A-Za-z0-9_]+),\s*([A-Za-z0-9_]+),\s*([A-Za-z0-9_]+),\s*([A-Za-z0-9_]+)\r?\n\s*msgbox\s+([A-Za-z0-9_]+),\s*([A-Za-z0-9_]+)\r?\n\s*end') { $eventScript = $matches[1] $trainer = $matches[2] $introText = $matches[3] $defeatText = $matches[4] $notenoughmons = $matches[5] $postbattle = $matches[6] $messagebox = $matches[7] $replacement = "$eventScript:: trainerbattle_double $trainer, $introText, $defeatText, $notenoughmons specialvar VAR_RESULT, ShouldTryRematchBattle goto_if_eq VAR_RESULT, TRUE, $eventScript`Rematch msgbox $postbattle, $messagebox end $($eventScript)Rematch:: trainerbattle_rematch_double $trainer, $introText, $defeatText, $notenoughmons msgbox $postbattle, $messagebox end" $content = $content -replace [regex]::Escape($item), $replacement } } # ---------------------------- # Write back to file # ---------------------------- if($new -ne $content) { Set-Content -Path $_.FullName -Value $content Write-Host "Updated: $($_.FullName)" } }