1

I am writing a bat script to create a project directory and then create dotnet projects. In the script, I try to access the csproj file through referencing it. The issue that I am having is that the directory name is not being pulled in. My bat script takes two inputs, the project directory and project name.

echo %1
md %1
cd %1

echo %2
::Create Directory Structure

md src
md documentation
md "documentation/SystemDocuments"
md "documentation/Diagrams"

::Create Solution Structure
cd src
dotnet new sln --name %2
dotnet new console -o %2
set "test=%2%\%2%.csproj"
echo %test% :: This value is wrong
echo %2%\%2%.csproj
dotnet sln add %2%\%2%.csproj

So if I run the above code with input 'some directory' testProject, I would expect the first and second echo to be testProject\testProject.csproj and it is testProject.csproj. What am I doing wrong in the string concatenation?

Example Run of program: CMD: ProjectCreation.bat D:\repos\ProjectSetup\Test TestApp

Terminal Output:

    D:\repos\ProjectSetup>echo D:\repos\ProjectSetup\Test
D:\repos\ProjectSetup\Test

D:\repos\ProjectSetup>md D:\repos\ProjectSetup\Test

D:\repos\ProjectSetup>cd D:\repos\ProjectSetup\Test

D:\repos\ProjectSetup\Test>echo TestApp
TestApp ::Output of %2

D:\repos\ProjectSetup\Test>md src

D:\repos\ProjectSetup\Test>md documentation

D:\repos\ProjectSetup\Test>md "documentation/SystemDocuments"

D:\repos\ProjectSetup\Test>md "documentation/Diagrams"

D:\repos\ProjectSetup\Test>cd src

D:\repos\ProjectSetup\Test\src>dotnet new sln --name TestApp
The template "Solution File" was created successfully.

D:\repos\ProjectSetup\Test\src>dotnet new console -o TestApp
The template "Console App" was created successfully.

Processing post-creation actions...
Running 'dotnet restore' on D:\repos\ProjectSetup\Test\src\TestApp\TestApp.csproj...
  Determining projects to restore...
  Restored D:\repos\ProjectSetup\Test\src\TestApp\TestApp.csproj (in 111 ms).
Restore succeeded.


D:\repos\ProjectSetup\Test\src>set "test=TestApp2.csproj"

D:\repos\ProjectSetup\Test\src>echo TestApp2.csproj
TestApp2.csproj

D:\repos\ProjectSetup\Test\src>echo TestApp2.csproj
TestApp2.csproj

D:\repos\ProjectSetup\Test\src>dotnet sln add TestApp2.csproj
Could not find project or directory `TestApp2.csproj`.
Description:
  Add one or more projects to a solution file.

Usage:
  dotnet [options] sln <SLN_FILE> add [<PROJECT_PATH>...]

Arguments:
  <SLN_FILE>      The solution file to operate on. If not specified, the command will search the current directory for one. [default: D:\repos\ProjectSetup\Test\src\]
  <PROJECT_PATH>  The paths to the projects to add to the solution.

Options:
  --in-root                                Place project in root of the solution, rather than creating a solution folder.
  -s, --solution-folder <solution-folder>  The destination solution folder path to add the projects to.
  -?, -h, --help                           Show command line help.
2
  • What exactly is the command for executing the .bat file? Add at the beginning echo %2, and show us the entire output generated by the call in the Command Prompt.
    – harrymc
    Commented Feb 16, 2022 at 16:19
  • Updated with echo %2 and gave output of terminal.
    – Nate
    Commented Feb 16, 2022 at 16:25

1 Answer 1

1

The problem is this command:

set "test=%2\%2.csproj"

The problem was using %2% instead of %2.

1
  • Thank you, that fixed the issue.
    – Nate
    Commented Feb 16, 2022 at 16:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .