2

I have a syntax error in this Batch script, but I have no idea where it is coming from. I'm new to batch so I'm having trouble figuring this out. I have a feeling it's something to do with the if statement, but am unsure.

@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

The current error I get is:

The syntax of the command is incorrect.

Jimenemex
  • 3,104
  • 3
  • 24
  • 56

3 Answers3

2

The syntax of the commented out COPY command is causing the problem. By changing the comment from :: to rem as shown below the script will run.

The command interpreter appears to be getting confused between a label : and a comment ::.

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
)
miltonb
  • 6,905
  • 8
  • 45
  • 55
  • That's because `::` isn't a comment but an illegal label. – ACatInLove Jan 09 '18 at 23:47
  • I see, so `::` is a hack which in some cases appears to work like a comment. But in this case it does not. I would suggest standardize on rem. – miltonb Jan 09 '18 at 23:51
  • Yes that is correct. One skill of a programmer is to understand the contract. The docs say if you do this then this will happen. The docs say nothing about `::` as a comment and `REM` is documented - So it will always work, even if there is a massive upgrade to cmd.exe (last upgrade was 2000). EG `REM` works on DOS command.com, OS/2 cmd.exe, NT4 cmd.exe, and the current Win2000 cmd.exe. – ACatInLove Jan 10 '18 at 00:21
2

The interpreter will read from the line with the opening brace right up to the line with the closing brace and treat it like one big line. Using echo on should help to show you this and help to locate any line that may cause error.

You could use goto to avoid making such a huge if block of code enclosed by braces.

Using goto also helps to avoid issues such as any need delayed expansion of variables, comment issues etc.

@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

Set echo off once you know any issues are fixed.

michael_heath
  • 5,262
  • 2
  • 12
  • 22
1

try like this:

@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

More information here

npocmaka
  • 55,367
  • 18
  • 148
  • 187
  • Using double colons for comments is foolish. – jwdonahue Jan 10 '18 at 05:18
  • @jwdonahue - Admittedly it's strange and usually doesn't work, but npocmaka has forgotten more about batch than you will ever learn so I trust him. – SomethingDark Jan 10 '18 at 07:03
  • @jwdonahue - it is not foolish - `::` comments [are faster](https://stackoverflow.com/questions/12407800/which-comment-style-should-i-use-in-batch-files) than `rem` , and look better. Though if the last line in brackets block is commented with `::` it may cause problems. – npocmaka Jan 10 '18 at 08:56
  • @npocmaka, looks better is subjective. Most good coders find them jarring, non-standard and the cause of many beginners errors, such as the OP's problem. As for faster, that is marginal at best, given that most cmd scripts are not lengthy expose's of rem statements, the boost in speed for the average script probably won't ever compensate for the time spent helping coders out of situations such as the OP's experience with them. – jwdonahue Jan 10 '18 at 22:44