批处理脚本语法不正确

时间:2018-01-09 22:05:16

标签: windows batch-file

我在这个Batch脚本中有一个语法错误,但我不知道它来自哪里。我是批处理的新手,所以我很难搞清楚这一点。我觉得它与if声明有关,但我不确定。

@ECHO Off

:: Variables
@SET UI_Debug_Path="rootpath"

@SET CoreDev_Bin="destinationPath1"
@SET Website_Bin="destinationPath2"

@SET Business_DLL="Business.dll"
@SET Business_PDB="Business.pdb"
@SET DataAccess_DLL="DataAccess.dll"
@SET DataAccess_PDB="DataAccess.pdb"
@SET UI_DLL="Forms.dll"
@SET UI_PDB="Forms.pdb"

@SET doCopy=n
:: Prerequisite
echo Ensure you have permission/access to files!
SET /P doCopy=Copy Files (y\n) (Default - n)?

IF /I "%doCopy%"=="Y" (
    :: .DLLs
    echo "Copying .DLLs to CoreDev\Bin and Website\Bin"

    COPY %UI_Debug_Path%%Business_DLL% %CoreDev_Bin%

    COPY %UI_Debug_Path%%Business_DLL% %Website_Bin%

    :: COPY %UI_Debug_Path%%DataAccess_DLL% %CoreDev_Bin% :: Don't think we need DataAccess.dll copied in CoreDev\bin

    COPY %UI_Debug_Path%%DataAccess_DLL% %Website_Bin%

    COPY %UI_Debug_Path%%UI_DLL% %CoreDev_Bin%

    ::COPY %UI_Debug_Path%%UI_DLL% %Website_Bin% :: Don't think we need UI.dll in Website\bin


    :: .PDB files
    echo "Copying .PDB to CoreDev\Bin and Website\Bin"
    COPY %UI_Debug_Path%%Business_PDB% %CoreDev_Bin%

    COPY %UI_Debug_Path%%Business_PDB% %Website_Bin%

    ::COPY "%UI_Debug_Path%%DataAccess_PDB% %CoreDev_Bin% :: Don't think we need DataAccess.pdb in CoreDev\bin

    COPY %UI_Debug_Path%%DataAccess_PDB% %Website_Bin%

    COPY %UI_Debug_Path%%UI_PDB% %CoreDev_Bin%

    ::COPY %UI_Debug_Path%%UI_PDB% %Website_Bin% :: Don't think we need UI.pdb in WebSite\bin
)

Pause

我得到的当前错误是:

  

该命令的语法不正确。

3 个答案:

答案 0 :(得分:2)

注释掉的COPY命令的语法导致了问题。通过将评论从::更改为rem,如下所示,脚本将会运行。

命令解释器似乎在标签:和评论::之间混淆。

IF /I "%doCopy%"=="Y" (
  :: .DLLs
  echo "Copying .DLLs to CoreDev\Bin and Website\Bin"

  COPY %UI_Debug_Path%%Business_DLL% %CoreDev_Bin%

  COPY %UI_Debug_Path%%Business_DLL% %Website_Bin%

  rem :: COPY %UI_Debug_Path%%DataAccess_DLL% %CoreDev_Bin% :: Don't think we need DataAccess.dll copied in CoreDev\bin
)

答案 1 :(得分:2)

解释器将从带有开口支撑的线上读到直到带有闭合支撑的线,并将其视为一条大线。使用echo on应该有助于向您展示并帮助找到可能导致错误的任何行。

您可以使用goto来避免使用大括号括起这么大的if代码块。

使用goto也有助于避免诸如任何需要延迟扩展变量,评论问题等问题。

@ECHO On

:: Variables
@SET UI_Debug_Path="rootpath"

@SET CoreDev_Bin="destinationPath1"
@SET Website_Bin="destinationPath2"

@SET Business_DLL="Business.dll"
@SET Business_PDB="Business.pdb"
@SET DataAccess_DLL="DataAccess.dll"
@SET DataAccess_PDB="DataAccess.pdb"
@SET UI_DLL="Forms.dll"
@SET UI_PDB="Forms.pdb"

@SET doCopy=n
:: Prerequisite
echo Ensure you have permission/access to files!
SET /P "doCopy=Copy Files (y\n) (Default - n)? "

IF /I NOT "%doCopy%"=="Y" GOTO :next

:: .DLLs
echo "Copying .DLLs to CoreDev\Bin and Website\Bin"

COPY %UI_Debug_Path%%Business_DLL% %CoreDev_Bin%

COPY %UI_Debug_Path%%Business_DLL% %Website_Bin%

:: COPY %UI_Debug_Path%%DataAccess_DLL% %CoreDev_Bin% :: Don't think we need DataAccess.dll copied in CoreDev\bin

COPY %UI_Debug_Path%%DataAccess_DLL% %Website_Bin%

COPY %UI_Debug_Path%%UI_DLL% %CoreDev_Bin%

::COPY %UI_Debug_Path%%UI_DLL% %Website_Bin% :: Don't think we need UI.dll in Website\bin


:: .PDB files
echo "Copying .PDB to CoreDev\Bin and Website\Bin"
COPY %UI_Debug_Path%%Business_PDB% %CoreDev_Bin%

COPY %UI_Debug_Path%%Business_PDB% %Website_Bin%

::COPY "%UI_Debug_Path%%DataAccess_PDB% %CoreDev_Bin% :: Don't think we need DataAccess.pdb in CoreDev\bin

COPY %UI_Debug_Path%%DataAccess_PDB% %Website_Bin%

COPY %UI_Debug_Path%%UI_PDB% %CoreDev_Bin%

::COPY %UI_Debug_Path%%UI_PDB% %Website_Bin% :: Don't think we need UI.pdb in WebSite\bin

:next

Pause

一旦您知道任何问题得到解决,请设置echo off

答案 2 :(得分:1)

尝试这样:

@ECHO Off

:: Variables
@SET UI_Debug_Path="rootpath"

@SET CoreDev_Bin="destinationPath1"
@SET Website_Bin="destinationPath2"

@SET Business_DLL="Business.dll"
@SET Business_PDB="Business.pdb"
@SET DataAccess_DLL="DataAccess.dll"
@SET DataAccess_PDB="DataAccess.pdb"
@SET UI_DLL="Forms.dll"
@SET UI_PDB="Forms.pdb"

@SET doCopy=n
:: Prerequisite
echo Ensure you have permission/access to files!
SET /P doCopy=Copy Files (y\n) (Default - n)?

IF /I "%doCopy%"=="Y" (
    :: .DLLs
    echo "Copying .DLLs to CoreDev\Bin and Website\Bin"

    COPY %UI_Debug_Path%%Business_DLL% %CoreDev_Bin%

    COPY %UI_Debug_Path%%Business_DLL% %Website_Bin%

    :: COPY %UI_Debug_Path%%DataAccess_DLL% %CoreDev_Bin% :: Don't think we need DataAccess.dll copied in CoreDev\bin

    COPY %UI_Debug_Path%%DataAccess_DLL% %Website_Bin%

    COPY %UI_Debug_Path%%UI_DLL% %CoreDev_Bin%

    ::COPY %UI_Debug_Path%%UI_DLL% %Website_Bin% :: Don't think we need UI.dll in Website\bin


    :: .PDB files
    echo "Copying .PDB to CoreDev\Bin and Website\Bin"
    COPY %UI_Debug_Path%%Business_PDB% %CoreDev_Bin%

    COPY %UI_Debug_Path%%Business_PDB% %Website_Bin%

    ::COPY "%UI_Debug_Path%%DataAccess_PDB% %CoreDev_Bin% :: Don't think we need DataAccess.pdb in CoreDev\bin

    COPY %UI_Debug_Path%%DataAccess_PDB% %Website_Bin%

    COPY %UI_Debug_Path%%UI_PDB% %CoreDev_Bin%

    rem COPY %UI_Debug_Path%%UI_PDB% %Website_Bin% :: Don't think we need UI.pdb in WebSite\bin
)

Pause

更多信息here