0

I am looking for a way to automate a manual task. I'm not sure if it's even possible.

I have to find a pattern of string in all files in a project folder. It'a project of C#/.net project(if that matters at all). I have to also write the function name and file name where the pattern occurs, along with the full string that matches it. So far I've done following in PowerShell:

PS C:\trunk> Get-ChildItem "C:\trunk” -recurse | Select-String -pattern
“AlertMessage” | group path | select name

This prints file name where string pattern matches.

PS C:\trunk> Select-String -pattern "AlertMessage" -path
"C:\trunk\VATScan.Web\Areas \Administration\Controllers\HomeController.cs”

This prints line number and string that matches it in a given file.

Any pointers on how I can acheive my goal?

3
  • Write the function name? Do you mean identifying the C# function where AlertMessage is invoked? Commented Aug 21, 2014 at 10:06
  • @DavidBrabant That's right. I'm not sure if that's possible. Commented Aug 21, 2014 at 10:07
  • Well, probably not possible with PowerShell only. Using a combination of Roslyn and PowerShell, maybe. But is it worth the effort? Commented Aug 21, 2014 at 10:12

1 Answer 1

1

By no means perfect but at least this my fall under the category of pointer

$text = @"
Public Sub Bitchin()
    Dim AlertMe
End Sub

Private Sub Function() As something

End Function
"@

[void]($text -match "(?smi)((public|private)\W(sub|function)\W(.+?)\(.*?Alertme)")
$Matches[4]

This will look for a Function or Sub routine declaration with a single white space between words followed by the next occurrence of the word AlertMe

Need to get item 4 from $Matches since there are a bunch of capture groups.

A more concise explanation of the regex used can be found here

Hopefully this will get you started or at least thinking. I am not familiar with c# declarations as $text is more of a VBA example but your should get the idea.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.