bat 檔案名稱不是預期的

bat 檔案名稱不是預期的

我正在編寫一個bat腳本來建立專案目錄,然後建立dotnet專案。在腳本中,我嘗試透過引用來存取 csproj 檔案。我遇到的問題是目錄名稱沒有被拉入。

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.

相關內容