12

Following examples from here I'm trying to execute

dotnet sln AllProjects.sln add **/*.csproj

But I get this error:

Could not find project or directory **/*.csproj.

Looks like wildcards are not working. What am I doing wrong?

astef
  • 8,575
  • 4
  • 56
  • 95

4 Answers4

26

For Windows, open PowerShell and run this command to add all projects to the solution file:

 dotnet sln add (ls -r **/*.csproj)
Matt Sage
  • 269
  • 1
  • 3
  • 4
  • How can this be done in Linux? – jvbs Jun 14 '22 at 03:04
  • 1
    Running the above command throws the error: _syntax error near unexpected token `('_ because the '$' before the _ls_ is missing. The correct syntax is: _dotnet sln add $(ls -r **/*.csproj)_ which is working on (Git and Linux) bash. – Cesar Urdaneta Mar 03 '23 at 15:47
10

I've missed this statement:

Globbing patterns are supported on Unix/Linux based terminals

My Windows PowerShell solution looks like this:

$projects = Get-ChildItem -Recurse | Where-Object { $_.Name -match '^.+\.(csproj|vbproj)$' }

$uniqueProjects = $projects | Group-Object -Property Name | Where Count -EQ 1 | select -ExpandProperty Group | % { $_.FullName }

Invoke-Expression -Command "dotnet new sln -n AllProjects"

$uniqueProjects | % { Invoke-Expression -Command "dotnet sln AllProjects.sln add ""$_""" }
astef
  • 8,575
  • 4
  • 56
  • 95
  • 1
    This should be voted down. Way too much work. The right answer is below: dotnet sln add (ls **/*.csproj) – Quark Soup May 01 '23 at 00:14
  • Official documentation contains all the solutions much better than this. https://learn.microsoft.com/en-us/dotnet/core/tools/dotnet-sln – Branislav B. May 26 '23 at 19:57
6

On Windows you could also use the following command to recursively add all the projects in sub-directories to a pre-existing solution file:

FOR /R %i IN (*.csproj) DO dotnet sln add "%i"

Alternatively, if you need to (re)create solution files often then you could create a batch file with the following content, and then just run it whenever you need to:

dotnet new sln 
FOR /R %%i IN (*.csproj) DO dotnet sln add "%%i"

Please note that you need the extra % when trying to do this inside the batch file.

Gordon Rudman
  • 420
  • 4
  • 7
2

I tried ls -r on git bash

dotnet sln add (ls -r **/*.csproj)

but it gives me

$ dotnet sln add (ls -r **\*.csproj)
bash: syntax error near unexpected token `('

And then i tried

dotnet sln add **/*.csproj

it worked for me on git bash(windows)

HBA114
  • 17
  • 5
  • The simbol $ before the (ls...) is missing. – Cesar Urdaneta Mar 03 '23 at 15:49
  • On that section i wrote command with console output and that $ sign is not from command. The command you should execute is first command or last command in this answer. Second one is output of first one – HBA114 Mar 04 '23 at 16:29