Tuesday, February 11, 2014

Formatting all files in a solution in Visual Studio

When I had to format all files in a solution,
all I could find at first were macros. Since Visual Studio 2012 doesn't have the macro explorer anymore,
I had to find an other way to accomplish this.

Thanks to this github page I found the follow code that accomplishes just what I need.
Open the Nuget package manager console and paste in (2 lines) :

function f($projectItems) { $projectItems | ? { $_.Name.EndsWith( ".cs" ) } | % { $win = $_.Open('{7651A701-06E5-11D1-8EBD-00A0C90F26EA}') ; $win.Activate() ; $DTE.ExecuteCommand('Edit.FormatDocument') } ; if ($projectItems) { $projectItems | % { f($_.projectItems) } } }

$dte.Solution.Projects | % { f($_.ProjectItems) }
Thanks JayBazuzi!