Search notes:

PowerShell: Search and replace text in a directory

The following example tries to demonstrate how text can be searched/replaced in files in a directory and its subdirectories.
I use the encoding iso-8859-1 so that I can replace files in both, latin-1 and UTF-8 encoded files (as long as the text searched for and replaced with is does not have characters that cannot be represented with latin-1?).
$enc = [System.Text.Encoding]::GetEncoding('iso-8859-1')

get-childItem -recurse -attribute !d *.sql | % {

   $fileContentBytes = [System.IO.File]::ReadAllBytes($_.FullName)
   $fileContent      = $enc.GetString($fileContentBytes)

   $fileContent      = $fileContent -replace 'rabenschwarz', 'hellgelb'

   $fileContentBytes = $enc.GetBytes($fileContent)
  [System.IO.File]::WriteAllBytes($_.FullName, $fileContentBytes)

}

See also

In place editing

Index

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 8 attempt to write a readonly database in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php:78 Stack trace: #0 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(78): PDOStatement->execute(Array) #1 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(30): insert_webrequest_('/notes/Windows/...', 1759398441, '216.73.216.42', 'Mozilla/5.0 App...', NULL) #2 /home/httpd/vhosts/renenyffenegger.ch/httpsdocs/notes/Windows/PowerShell/examples/search-and-replace-text-in-directory(54): insert_webrequest() #3 {main} thrown in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php on line 78