Thursday, February 2, 2012

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 comment

Fill out the form below to add your own comments


Bookmark Site

Bookmark and Share

Tags

  For Sale     Perl     Availability     General     Contracting     C#     Technology     PHP     FlatPress  

Search

Admin area