Overwrite a specific line in multiple files with PowerShell with user input

I am trying to get my PowerShell Script to read a specific line of 8 in different files .ctlin several folders and overwrite it using the finished text and the input made by the user in the text box.

PowerShell Script:

$handler_button3_Click={
if ($textbox1.TextLength -eq 0)
{
    $listBox1.Items.Add("Please Register your Release Number!")
}else{
    #saving the number in releasenr
    $releasenr = $textbox1.Text

    #TODO: Loop which goes into every file on different Folders and replaces
    #line 8 in all .ctl files with following text:     "rel_nr    constant "$releasenr""

    $listBox1.Items.Clear()
    $listBox1.Items.Add("Release Number has been overwritten")
    $listBox1.Items.Add("You can now proceed your Upload")
}
}

Is there a way to use the for loop to execute the foreach rewrite process that is in the current folder?

+4
source share
1 answer

Get-ChildItem , ForEach-Object, , Get-Content, , , Set-Content:

Get-ChildItem 'yourFolder' -Filter '*.ctl' | ForEach-Object {
    $content = Get-Content $_
    $content[7] = 'rel_nr    constant "{0}"' -f $releasenr
    $content | Set-Content -Path $_.FullName
}
+2

Source: https://habr.com/ru/post/1650760/


All Articles