CodePrairie .NET

South Dakota .NET User Group


Using Powershell to see how much work it will be to replace several calls.

I was using the brail view engine yesterday and ran into a problem. So I wanted to be able to find all the places I was calling ToString( ) on a variable and passing in a format. Powershell makes this nice and easy.


gci -recurse -include *.brail | Select-String "ToString"

So here's what happens.
gci is an alias for get-childitem (you can also use dir or ls)
I tell it i want to recurse and only check files with the .brail file extension.

Next I pipe ( | ) the output to the select-string commandlet which will find all matches in each file.
The output of select-string looks something like this

Ebp\Statement\viewsummary.brail:99: <dd class="Charge"><strong>${total.ToStri

But this is not that useful for what I'm trying to do, so I run this command:

gci - recurse -include *.brail | Select-String "ToString" | Format-List

Which yields something a bit more useful:


IgnoreCase : True
LineNumber : 3
Line : ${item.OccurredOn.ToString("d")},${item.OccurredOn.ToString("T")},${item.FromPl
Filename : DownloadUsageDetails.brail
Path : C:\develop\ecrm\source\Martin.eCRM.Web\Views\Ebp\Statement\DownloadUsageDetails
Pattern : ToString

IgnoreCase : True
LineNumber : 99
Line : <dd class="Charge"><strong>${total.ToString("c")}</strong></dd>
Filename : viewsummary.brail
Path : C:\develop\ecrm\source\Martin.eCRM.Web\Views\Ebp\Statement\viewsummary.brail
Pattern : ToString

However I have a lot of files so in reality this list is much longer, but what Format-List told me was that the output of select-string created objects with a FileName property so I change my command to be:


gci -recurse -include *.brail | Select-String "ToString" | group Filename

Which gave me this nice output:

Count Name Group
----- ---- -----
1 DownloadUsageDetails.b... {DownloadUsageDetails.brail}
1 viewsummary.brail {viewsummary.brail}
3 _usagedetailstable.brail {_usagedetailstable.brail, _usagedetailstable.brail, _usa...
8 index.brail {index.brail, index.brail, index.brail, index.brail, inde...
3 paymenthistory.brail {paymenthistory.brail, paymenthistory.brail, paymenthisto...
1 EditPackage.brail {EditPackage.brail}
1 ListFeatures.brail {ListFeatures.brail}
4 OrderSummary.brail {OrderSummary.brail, OrderSummary.brail, OrderSummary.bra...

And now I can look at which files need editing and how many occurrences there are.


Published May 18 2007, 07:51 AM by chrisortman
Filed under:


No Comments

Leave a Comment

Powered by Community Server (Commercial Edition), by Telligent Systems