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
- The Get-Content cmdlet is bracketed to ensure the file is finished with before the Set-Content cmdlet is called.
- 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