1

I am hoping to leverage powershell in visual studio to add multiple files that have been dropped in a project directory to the project. I have tried a few things to get this going, using hints found in this thread but am still running into a wall.

This first attempt looks in the solution directory for any .cs files and tries to add them to the solution (not sure if that is really correct, as I want them added to the project, not the solution).

Split-Path -Parent -Path $dte.Solution.FullName | '
Get-ChildItem -Include @("*.cs") -Recurse | '
%{ [string]$_.FullName; $dte.Solution.AddFromFile([string]$_.FullName) }

This throws an error: Exception calling "AddFromFile" with "1" argument(s): "The template specified cannot be found. Please check that the full path is correct." I am writing out the path to the file before I add it so I am able to verify that it is the correct path.

I have also tried to use ExecuteCommand to add an existing item to the project thusly

Split-Path -Parent -Path $dte.Solution.FullName | '
Get-ChildItem -Include @("*.cs") -Recurse | '
%{ $dte.ExecuteCommand("Project.AddExistingItem", $_.FullName) }

The error here is: Exception calling "ExecuteCommand" with "2" argument(s): "Command "Project.AddExistingItem" does not accept arguments or switches."

Has anyone tried to do something similar and had success?

Community
  • 1
  • 1
gwoody1984
  • 315
  • 3
  • 16
  • One other important point to note. I have tried both methods with a hardcoded, known good path to a file and both have failed – gwoody1984 Jun 13 '14 at 12:35
  • To add files you need to use Project.ProjectItems.AddFromFile. See http://stackoverflow.com/questions/2000197/visual-studio-macro-find-files-that-arent-included-in-the-project – Sergey Vlasov Jun 14 '14 at 10:02
  • I can't find this exposed in PowerShell. I have found a different path to the same method, however: $dte.ItemOperations.AddFromFile(file) – gwoody1984 Jun 16 '14 at 12:26

1 Answers1

0

I have found a solution which I think solves the problem nicely. The command I am using is:

 Split-Path -Parent -Path $dte.Solution.FullName | `
 Get-ChildItem -Include @("*.cs") -Recurse | `
 Select-Object FullName | `
 %{ $dte.Application.ItemOperations.AddExistingItem($_.FullName) }
gwoody1984
  • 315
  • 3
  • 16