1

What is the different between Empty String and none Created Variable in Batch File? and if they have any different, can you show me an example of using empty string vs none created string?

@echo off
title Empty String Vs None Created Variable
set String=
set variable=exist
if [%String%] == [] echo its be a Empty String
if [%variable%] == [] echo its be a Empty String
if [%none_exist_var%] == [] echo its be a Empty String

Thanks a Lot!

Mohammad Barghamadi
  • 107
  • 1
  • 1
  • 10
  • There is no way to define a variable with an empty string in Batch. `set "var="` command _deletes_ the variable. – Aacini Feb 11 '17 at 15:39
  • @Aacini it's possible to [set a variable to null](https://en.wikipedia.org/wiki/Batch_file#Null_values_in_variables). That's why you need quotes or some other text when comparing in `if`, otherwise `if %var%==text` might fail if `%var%` contains nothing. [How to set/use an empty string value in a variable](http://superuser.com/q/689847/241386) – phuclv Feb 12 '17 at 15:36
  • @LưuVĩnhPhúc: The linked Wikipedia article is not clear. As I said before, **there is no way to define a variable with an empty string in Batch**. I invite you to test this command: `if defined var echo Value = "%var%"` and report when you get this result: `Value = ""` – Aacini Feb 12 '17 at 19:59
  • @Aacini I've just checked and [undefined variables indeed expand to an empty string in batch files](http://stackoverflow.com/a/4095133/995714) so you can just delete the variable and use it afterwards as null – phuclv Mar 06 '17 at 05:32
  • @LưuVĩnhPhúc: I don't understand how your last comment is related to this topic. I stated that **There is no way to define a variable with an empty string in Batch. `set "var="` command _deletes_ the variable**, but in your first comment you said: _"it's possible to set a variable to null"_. These two points are opposite and just one can be right (and the other one is wrong). Do you have any additional comment about _this topic_? – Aacini Mar 06 '17 at 14:52

2 Answers2

2

Variables in batch files can be

  • Defined: there is a value stored and a associated name used to retrieve the value, that is, the variable name.
  • Undefined: there is no value and in consecuence there is not any need for an associated name, so it does not exist.

This two simple rules handle how the environment block (where variables/value are stored) is updated. If there is a value, the environment block has an entry for the value and the name to retrieve it. Without a value, the environment block does not create the entry or, when setting the variable to nothing, the entry is removed.

So, if

  • a never defined variable has not any entry in the environment block
  • a variable with not value has not any entry in the environment block

there is not any difference between the two cases.

note: While the traditional way to check if a variable stores a value / a variable exists is (as dbenham has commented, this syntax is not recommeded as quotes inside the variable value lead to syntax problems)

if "%varName%"=="" ....

if command extensions are enabled (and the default configuration is to have them enabled) you can also use a safer alternative

if not defined varName ....
if defined varName ....

note that in this syntax, as we are not trying to read the value in the variable, varName is used, not %varName%

MC ND
  • 69,615
  • 8
  • 84
  • 126
  • 1
    I would use "traditional" or "historical" instead of "usual". The `if "%varname%" == ""` syntax can fail if the value contains its own quotes. That is a major reason why the `if defined varName` syntax was invented - it always succeeds regardless of value. `if "%varName%" == ""` should be avoided. – dbenham Feb 12 '17 at 15:11
  • @dbenham, I used *usual* in the sense of what I usually see in posted code, what I see as *habitual*. I know the problems behind this usage. Any way, I like the proposed *traditional* term, thank you. Answer updated. – MC ND Feb 12 '17 at 15:26
  • Yes, I understood your use of "usual", and it was not wrong. But I like your edited version better :-) – dbenham Feb 12 '17 at 17:11
1

There is no difference. Read Delete a variable:

Type SET with just the variable name and an equals sign:

SET _department=

Better still, to be sure there is no trailing space after the = place the expression in parentheses or quotes:

(SET _department=)

or

SET "_department="
JosefZ
  • 28,460
  • 5
  • 44
  • 83