Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

Rename multile lines in files

$
0
0

Hello,

 i have a script that should rename several lines in multiple files in a directory.

Here is the script:

$stringtofind = 'a1a1a1'
$replacewith = 'a2a2a2'
$stringtofind1 = 'b1b1b1'
$replacewith1 = 'b2b2b2'
$stringtofind2 = 'c1c1c1'
$replacewith2 = 'c2c2c2'

Set-Location D:\files
gci -Filter *.sql | select -expand fullname | where { !$_.PSIsContainer } | % {
    $file = get-content   $_
    $containsword = $file | %{ $_ -match $stringtofind }
    $containsword1 = $file | %{ $_ -match $stringtofind1 }
    $containsword2 = $file | %{ $_ -match $stringtofind2 }

          if ($containsword -contains $true)
            #$containsword1 -contains $true `
            #$containsword2 -contains $true)
        
        {
             (ForEach {
             echo "SQL file"  $_  "need to be modified."
             echo "Edit in progress ..."
             (Get-Content $_ |                   
             foreach-object {
              $_ -replace $stringtofind , $replacewith `
                 -replace $stringtofind1, $replacewith1 `
                 -replace $stringtofind2, $replacewith2}) |
             Set-Content $_ })
             echo " The selected String in" $_  "file has been renamed"
        }
        else 
        {
            
             echo "Start executing SQL scripts"
        }  
        }  

 

The problem is that the script modifies just one of the lines, the first pattern and i cant find the reason why it does not change the second and third pattern.

Can someone help here.


Viewing all articles
Browse latest Browse all 6937

Trending Articles