我正在编写一个 bat 脚本来创建项目目录,然后创建 dotnet 项目。在脚本中,我尝试通过引用 csproj 文件来访问它。我遇到的问题是目录名称未被提取。我的 bat 脚本接受两个输入,即项目目录和项目名称。
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
因此,如果我使用输入“某个目录” testProject 运行上述代码,我期望第一个和第二个回显是 testProject\testProject.csproj,并且它是 testProject.csproj。我在字符串连接中做错了什么?
程序运行示例:CMD:ProjectCreation.bat D:\repos\ProjectSetup\Test TestApp
终端输出:
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.
答案1
问题在于这个命令:
set "test=%2\%2.csproj"
问题在于使用%2%
而不是%2
。