A recent project has been to change the case of portions of text within a set of files.

This can be achieved by the following script

foreach ($file in Get-ChildItem *.php) 
    (Get-Content $file) | 
        [Regex]::Replace($_, 'href=".+?"', {param($m) $m.Value.toLower()}) 
    } | 
    Set-content $file

Two items to note

  1. The Get-Content cmdlet is bracketed to ensure the file is finished with before the Set-Content cmdlet is called.
  2. By using PowerShell 2 it is possible to pass script blocks as a delegate. Consequently entries that match the RegEx pattern can be processed, in this case converted to their lower case equivalent
 February 2, 2012


Add a comment