Sublime Forum

Parsing: Replacement Regex from Folder-A to Folder-B

#1

hello, can I use a selected text with regex from File-1.txt (Folder-A) to make a replacement on File-2.txt (Folder-B) ?

For example, I want to copy with Regex some lines in File-1.txt (Folder-A) between the words START and FINNISH and I want to paste-it over this word PUT-HERE-THE-CODE on File-2.txt (Folder-B)

The syntax should be:

FIND:

(?s)(START).*(FINNISH) \ Folder-1

Replace on the:

$1 PUT-HERE-THE-CODE $2 \ Folder-2

Can be done in sublime text such a replacement ?

0 Likes

#2

only Notepad++ has a plugin made in Python that can resolve this problem.

But it is easier to use PowerShell from Windows10

$sourceFiles = Get-ChildItem 'c:\Folder1'  
$destinationFolder = 'c:\Folder2'

foreach ($file in $sourceFiles) {

$sourceContent = Get-Content $file.FullName -Raw
$contentToInsert = [regex]::match($sourceContent,"(?ms)<!--START-->(.+)<!--FINNISH-->").value
$destinationContent = Get-Content $destinationFolder\$($file.Name) -Raw
$destinationContent = $destinationContent -replace 'PUT-HERE-THE-CODE',$contentToInsert

Set-Content -Path $destinationFolder\$($file.Name) -Value $destinationContent -Encoding UTF8

} #end foreach file
0 Likes