When customers won’t conform to your standards…

When working with customer-facing systems, they do not always conform to your standards. A perfect example of this is folder and file names. When users create them, they like to do things like the following:

I like to put spaces in my folder names
I just do it because I think it does not matter to anyone else

With PowerShell, it does not matter as much as it did with string based scripting tools, but I still like a little conformity. My preference it to use CamelCase in my folder naming. While the users customers like their folder names, if I am working with the stuff on the back end, I will typically put it into CamelCase. A perfect example of this is naming groups used for assigning permissions to the folder in question. When my helpdesk person was looking at reconciling permissions on our network drives, the biggest problem was the inconsistency of the naming of the groups to the folder names. My report was saying the folder did not have the correct permissions because I couldn’t find the group that conformed to the folder name. Instead of having my helpdesk person go through and fix all the folder or group names to be the same, I rewrote my report and used this function to help me reconcile the messing customer folder names to my elegant group names. The amount of work that had to be done by my helpdesk person for this project was reduced by over 90%. Let us get to the heart of the matter.

function ConvertTo-CamelCase
   # String to convert to CamelCase
   [Parameter(Mandatory = $true,
		   Position = 0)]
Begin {}
Write-Verbose "CTCC:StartingValue: $TextString"
foreach ($String in $TextString)
Write-Verbose "CTCC:Value of String to process: $String"
If ($String.Contains(" "))
	Write-Verbose "CTCC:String has space(s): '$String'"
	$TextInfo = (Get-Culture).TextInfo
	$String = $TextInfo.ToTitleCase($String.ToLower()).Replace(" ", "")
	Write-Verbose "CTCC:OneStepCamelCase String: '$String'"
	Write-Output $String
	Write-Output $String
} #end If String has space
} #end ForEach TextString
} #end Process
End {}

You can find the full code on my GitHub account: Function-ConvertTo-CamelCase

The first thing I do is check to see if the provided string has a space in it. If not, I just return the original value untouched. If it does have a space, I then establish a TextInfo object to allow me to modify the provided string. I got this off of someone else’s blog article but that information has been lost through many edits and my changing coding standards (you know, learning my way through it). Since I know the starting string has space(s), I first convert it all to lowercase. Then I use the ToTitleCase method of the TextInfo object. And, finally, I use the Replace method to remove the spaces from the string. I then return the CamelCase’d string as the output of the function.

This is one of my earlier functions and I am sure there are things that can be improved. Additional functionality could be implemented to handle more situations. But, this meets my current needs so I haven’t put any more time into improving my function. I hope you enjoy.



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s