2

I am testing software which has settings in text files. Now i need to change a specific line in ~100 files.

I searched hours for it and i am close to a solution. But dont know how to get it done.

A solution in notepad++ would be nice, but i tried it with powershell with the following command:

# File to change
$file = *.dat

# Get file content and store it into $content variable
$content = Get-Content -Path $file

# Replace the line number 40 with "0"
$content[39] = '"0"'

# Set the new content
$content | Set-Content -Path $file

It changes the specific line, but it also adds the data of all the files, in all the files in the folder. So in case of 200 lines the files now have 20000 lines. Every file.

I want to change in all the files linenumber 40:

"0" change to "1"

Because there are multiple values with "0" on other lines, i only want to change line 40 in multiple files.

1 Answer 1

1

You probably have to iterate over these files. Example:

Get-ChildItem *.dat | ForEach-Object {
   $content = Get-Content -Path $_ 
   $content[39] = '"0"'
   $content | Set-Content -Path $_
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks alot. This works like a charm and saves me alot of time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.