0

Why does the following batch script fail? Is it because there are 2 sets of double quotes? How do I get this to work??

SET INCOME-CODE="test (bob, bob2)"

IF "%INCOME-CODE%"=="NULL" (

  SET APCINVOICE=%APCINVOICE%,

 ) ELSE (

   SET APCINVOICE=%APCINVOICE%%INCOME-CODE%,

 )
Rafael
  • 3,042
  • 3
  • 20
  • 36
venatic
  • 1
  • 2
  • 2
    Do you want to check if `INCOME-CODE` is defined? – npocmaka Jan 28 '16 at 14:14
  • Are you sure hyphens for variable names are allowed in batch files? – Daniel Alder Jan 28 '16 at 14:29
  • What do you mean by fail? The code definitely does not cause a failure. But yes, your string comparison will not work if your string has quotes and your then put quotes around the variable for the comparison. – Squashman Jan 28 '16 at 14:32
  • @DanielAlder the hyphen seems to be valid. The problem is indeed the presence of two consecutive quotation marks, as the OP suspected. This turns the IF statement to `IF ""test (bob, bob2)"" and because there are two quotes, the string is actually taken as three separate words. – Klitos Kyriacou Jan 28 '16 at 14:33
  • Hyphens are working fine in other variable names. By fail I mean the batch file just stop running. – venatic Jan 28 '16 at 14:51
  • Usually Incomde-Code comes in with no value but is shows as null, we are checking to see if there is a value there – venatic Jan 28 '16 at 14:56
  • @venatic if **INCOME-CODE** shows as null then it has a value. In batch if there is no value assigned to a variable then it becomes undefined. Essentially the variable does not exist. – Squashman Jan 28 '16 at 15:49
  • 1
    Use `SET "INCOME-CODE=test (bob, bob2)"` instead of `SET INCOME-CODE="test (bob, bob2)"`, so the `""` do not become part of the variable value; then the comparison should work... – aschipfl Jan 28 '16 at 16:26
  • @Squashman it failed for me... `(bob was unexpected at this time.` –  Jan 28 '16 at 18:26

1 Answers1

0

Two things.

First, your variable INCOME-CODE actually has quotes in it. Try this short demonstration:

@echo off
set TEST1="this is a string with spaces"
set "TEST2=this is a string with spaces"
echo %TEST1%
echo %TEST2%

Second, you are comparing to the literal string "NULL". There is no NULL value in batch. Please see this post for more details: What is the proper way to test if variable is empty in a batch file... What you probably want to do is compare to an empty string or use one of the methods in the question above if you're working with something more complex.

IF "%INCOME-CODE%"=="" ( ... )
Community
  • 1
  • 1