0

When I use SET with spaces in the text I receive an error message. Why? Note: this call is inside of another function, I am using setlocal enabledelayedexpansion set at beginning of file.

This works with no error (no spaces in SET text):

:: Is the host up?
CALL :TEST_HOST_UP %REMOTE_SYSTEM%
IF %ERRORLEVEL% NEQ 0 (
    set "RETVAL=Error_Unable_To_Reach_Host"
    GOTO :FINISHED
)

Causes an error "was unexpected at this time":

:: Is the host up?
CALL :TEST_HOST_UP %REMOTE_SYSTEM%
IF %ERRORLEVEL% NEQ 0 (
    set "RETVAL=Error Unable To Reach Host"
    GOTO :FINISHED
)
Mike Q
  • 6,716
  • 5
  • 55
  • 62
  • 3
    The error does not come from (just) the fragment you posted. Try to turn `echo on` and save some more context around the point of failure. – dxiv Apr 29 '20 at 23:12
  • @dxiv your pointers led me to the problem immediately. I turned 'echo on' and found that it was correctly assigning SET and jumping to :FINISHED. That section though had code without quotes around the string used in an IF. I fixed it like so if "[%RETVAL%]"=="[No_Errors_Found]" (. If you respond to the question formally, I will up vote and select it as the correct answer. – Mike Q Apr 30 '20 at 03:15
  • 2
    Glad it helped, but it was just what it was - a comment. If you want to post a self-answer with the details and the resolution I'll be happy to upvote it. – dxiv Apr 30 '20 at 04:00
  • 1
    Thanks again. I provided a self answer. @dxiv – Mike Q Apr 30 '20 at 13:22

1 Answers1

1

The error message "was unexepected at this time" was actually a problem in the IF statement somewhere else in the code:

if [%RETVAL%]==[No_Errors_Found] (

Which is a common concern in scripting languages such as batch or shell, where you must put quotes around your variables:

if "[%RETVAL%]"=="[No_Errors_Found]" (

Thanks to @dxiv's help, I was able to debug the problem immediately - mainly by adding 'echo on' command to the code to provide verbose output.

More information on debugging batch scripts can be found here: How can I debug a .BAT script?

Mike Q
  • 6,716
  • 5
  • 55
  • 62