RamblingRoss
The blog of Ross Fruen, a .NET consultant

Using PowerShell to change case in files

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) |
foreach
{
[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

Add a comment

If you want your comment to appear on this page please complete the form below. Your name and email address are optional, although the latter will be required if you want a response. Your email address will not appear against your comment and will only be used to correspond with yourself (where appropriate).

Thanks!

Thank you for submitting your comment, it will appear here after moderation is complete.

Sorry

There was a problem sending your comment, please try again.