Windows 批处理脚本的语法不正确

Windows 批处理脚本的语法不正确,windows,batch-file,Windows,Batch File,我在这个批处理脚本中有一个语法错误,但我不知道它是从哪里来的。我是新来的,所以我很难弄明白这一点。我觉得这与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="Bus

我在这个批处理脚本中有一个语法错误,但我不知道它是从哪里来的。我是新来的,所以我很难弄明白这一点。我觉得这与
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
我得到的当前错误是:

该命令的语法不正确

试着这样做:

@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

更多信息

注释掉的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
)

口译员将从带大括号的那一行读到带大括号的那一行,并将其视为一条大行。在上使用
echo应有助于向您展示这一点,并有助于找到可能导致错误的任何行

您可以使用
goto
来避免在用大括号括起来的
代码块中生成如此巨大的

使用
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

不要在注释中使用双冒号。它会更详细地显示当前位置的输出和所有这些变量的扩展。这是因为
::
不是注释,而是非法标签。我明白了,所以
::
是一种黑客行为,在某些情况下,它的工作方式就像一条评论。但在这种情况下,情况并非如此。我建议对rem进行标准化。是的,这是正确的。程序员的一项技能是理解合同。医生说,如果你这样做,那么这将发生。文档中没有任何关于
的注释,而且
REM
是有文档记录的,因此即使对cmd.exe进行了大规模升级(上一次升级是2000年),它也会一直工作。例如,
REM
在DOS command.com、OS/2 cmd.exe、NT4 cmd.exe和当前的Win2000 cmd.exe上工作。使用双冒号作为注释是愚蠢的。@jwdonahue-无可否认,这很奇怪,通常不起作用,但是Npcmaka已经忘记了很多关于批处理的事情,这是你们永远学不到的,所以我相信他。@jwdonahue-这并不愚蠢-
注释比
rem
,看起来更好。虽然如果括号中的最后一行用
注释,可能会导致问题。@npocmaka,看起来更好是主观的。大多数优秀的程序员发现它们不和谐、不标准,并且是许多初学者错误的原因,比如OP的问题。至于更快,这充其量只是一个边缘,因为大多数cmd脚本都不是rem语句中冗长的expose语句,平均脚本速度的提高可能永远无法弥补帮助程序员摆脱诸如OP使用它们的经验之类的情况所花费的时间。使用双冒号作为注释是愚蠢的。