1

I am trying to read a .sln file and extract the strings that contain the path to the .csproj within my solution.

The lines that contain the information that I am looking for look like this:

Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Project", "Project\Project.csproj", "{0DB516E6-4358-499D-BFBF-408F50A44E14}"

So, this is what I am trying:

$projectsInFile = Select-String "$slnFile" -pattern '^Project'
$csprojectsNames = $projectsInFile -replace ".+= `"(\S*) `""

Now, $csprojectsName contain the information that I am looking for, but also the rest of the string.

Just like this:

Project\Project.csproj", "{0DB516E6-4358-499D-BFBF-408F50A44E14}"

What is the best way to retrieve the name of the .csproj file without needing to manually cut the rest of the string?

Thank you

4
  • $projectsInFile -replace '.+= "(\S*) "(.*?)",.*"','$2' working on cleaner ways. Put the part you want to keep in a group. Eat up the whole string and just replace it with the second group which has the string you want. Could also consider splitting on commas.... but that would only be useful if the structure was the same Commented Jul 24, 2015 at 17:42
  • Thanks. But, what I need is the "Project\Project.csproj" section. No the project Commented Jul 24, 2015 at 17:44
  • Check it again. I used the wrong number in the replacement it was fixed about a minute ago in an edit. Sorry Commented Jul 24, 2015 at 17:45
  • Thanks. If you want post the answer and I'll mark it as solved Commented Jul 24, 2015 at 17:47

2 Answers 2

3

What you can do is capture the entire string and use a capture group in your replacement string thereby dropping the unneeded parts.

$csprojectsNames = $projectsInFile -replace '.+= "(\S*) "(.*?)",.*"','$2'

The second capture group is the data inbetween the quotes that follow = "Project", ".....". Since it is the second capture group we replace the entire with that group '$2'. Using single quotes ensure that PowerShell does not try to expand a variable.

Better approach

You might just be able to use [^"]*?\.csproj in select-string directly without having to do a secondary parse. That will match everything before .csproj that is not a quote so it wont gooble up too much.

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

1 Comment

Thanks so much for all the details and improved answer
0

You can use a group to capture the file path and then use the value of the group in as the replacement value. For instance:

$csprojectsNames = $projectsInFile -replace 'Project\(.*?\) = "Project", "(.*?)"', '$1'

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.