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 ?
Setting a variable value to single whitespace
Moderator: DosItHelp
Re: Setting a variable value to single whitespace
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).
Steffen
Code: Select all
set "var1= "
echo(%var1%
Re: Setting a variable value to single whitespace
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:
I can probably add a check if the variable is empty or not, but are there better methods ?
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= "
)
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:
)
)
Re: Setting a variable value to single whitespace
You do not use variable expansion with the IF DEFINED command.
Re: Setting a variable value to single whitespace
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
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:
... and then output the variables this way:
... 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
This means that the result of
Code: Select all
echo Values: !var1! !var2!
You may assign the separation space to the value of each variable:
Code: Select all
set "var1= %%x" FOR EXAMPLE
set "var2= %%y"
Code: Select all
echo Values:!var1!!var2!
Antonio
Re: Setting a variable value to single whitespace
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 ?
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 ?