Setting a variable value to single whitespace

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Birage
Posts: 7
Joined: 17 Feb 2019 08:32

Setting a variable value to single whitespace

#1 Post by Birage » 12 Mar 2019 04:59

I have a variable which I wish to set to empty or null, and then echo the value to a file, preferably a whitespace, as I do not want anything to be shown.

I set the variable to null by set var1= .

However, echo !var1! results in "Echo is OFF", which is due to var1 being undefined.

Is there a way to set a variable value to whitespace, and when the variable is echoed, whitespace will be printed ?

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Setting a variable value to single whitespace

#2 Post by aGerman » 12 Mar 2019 05:29

Two things that go together here. First is that you should enclose the assignment in quotes. Second ist that even then the ECHO command would output the echo status instead of the space because a space is no meaningful argument. Common solution is to write a dot directly after the ECHO command but it has some drawbacks. Better solution is to use a left parenthesis (even if it looks weird).

Code: Select all

set "var1= "
echo(%var1%
Steffen

Birage
Posts: 7
Joined: 17 Feb 2019 08:32

Re: Setting a variable value to single whitespace

#3 Post by Birage » 12 Mar 2019 07:09

Oh thanks a lot for the advice !

It works, but due to the logic in my script, it doesn't work. Perhaps I should have stated the logic at the start, my bad.

I wish to set var1 value to empty or null at the end of the loop, otherwise if there are no values in the file for the next iteration, it will echo the value of the previous iteration, which is not what I want.

The script in summary:

Code: Select all

for /L %%Z in (1,1,!counter!) do ( 

rem Do something... obtain value from file. File may not contain any values. 

set var1="1st value from file"
set var2="2nd value from file"

echo Values: !var1! !var2!

rem Reset variable values to empty
set "var1= "
set "var2= "

)
I can probably add a check if the variable is empty or not, but are there better methods ? :)

Code: Select all

rem If both variables have valid values
if defined !var1! (
    if defined !var2!(
        echo Values: !var1! !var2!
    )
)

rem If var1 is valid, but not var2
if defined !var1! (
    if not defined !var2!(
        echo Values: !var1!
    )
)

rem If both variables are not valid
if not defined !var1! (
    if not defined !var2!(
        echo Values:
    )
)

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: Setting a variable value to single whitespace

#4 Post by Squashman » 12 Mar 2019 08:01

You do not use variable expansion with the IF DEFINED command.

Birage
Posts: 7
Joined: 17 Feb 2019 08:32

Re: Setting a variable value to single whitespace

#5 Post by Birage » 12 Mar 2019 08:29

Squashman wrote:
12 Mar 2019 08:01
You do not use variable expansion with the IF DEFINED command.
Oh, thanks for the heads up, didn't know that :)

Any idea on how to go about resetting the variable back to a clean slate ?

Cheers.

Aacini
Expert
Posts: 1885
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Setting a variable value to single whitespace

#6 Post by Aacini » 12 Mar 2019 10:01

I am afraid you have a confusion here... In Batch files, a variable can be undefined, that is, it does not exists (like in set "var="), or it can contain any string, including a blank space (like in set "var= "). In any case, the variable !expansion! replaces it by the variable value: nothing if the variable is undefined, or the variable value otherwise (including a blank space).

This means that the result of

Code: Select all

echo Values: !var1! !var2!
is straigthforward. If var2 not exists, the line ends in a space. If var1 not exists, the value of var2 is preceded by two spaces. If both variables not exists, the line ends in two spaces. If you want to supress these spaces, you could use IF commands, OR:

You may assign the separation space to the value of each variable:

Code: Select all

set "var1= %%x"    FOR EXAMPLE
set "var2= %%y"
... and then output the variables this way:

Code: Select all

echo Values:!var1!!var2!
... so the otput line with not show any additional space. This method have the additional benefit that it does not require to "reset" the variables. Of course, if you want to process the variable values, you need to eliminate the first space: if "!var1:~1!" equ "XYZ" echo var1 is "XYZ"

Antonio

Birage
Posts: 7
Joined: 17 Feb 2019 08:32

Re: Setting a variable value to single whitespace

#7 Post by Birage » 12 Mar 2019 20:53

Ah thanks for clearing my confusion :)

I think my initial test case of echo !var1! VS what is in my script echo Values: !var1! !var2! further confused me..

There is no way to print the blank space in the case of echo !var1!, as a single blank space is not a valid value as stated by aGerman ?

Post Reply