#1 13 Feb 2020 05:25

Rekrul
Member
Registered: 17 Apr 2016
Posts: 69

Is it possible to create an empty variable that's usable?

Is it possible to create a valid variable that has nothing in it, but that acts as a placeholder?

In a script that downloads files using a command line program, I'd like to have the option to specify an optional prefix for the filenames. I thought that the easiest way would be to include a variable in the filename spec and if it was defined, that would be added to the start of the name and if it was empty, it just wouldn't print anything. Of course it adds %prefix% to the start of the name. Which means that I need to have two copies of the command and an IF statement to decide which one to use.

Can I create a null variable that will be treated as valid, but that won't print anything?

Offline

#2 13 Feb 2020 23:57

Simon Sheppard
Super Administrator
Registered: 27 Aug 2005
Posts: 1,055
Website

Re: Is it possible to create an empty variable that's usable?

If you set a variable equal to Null the variable will be deleted, so no.

You can set a variable equal to a space character, but I think what you really need is to just test the parameter (using IF) to see if it has been populated.

Offline

#3 15 Feb 2020 02:03

Rekrul
Member
Registered: 17 Apr 2016
Posts: 69

Re: Is it possible to create an empty variable that's usable?

Simon Sheppard wrote:

If you set a variable equal to Null the variable will be deleted, so no.

You can set a variable equal to a space character, but I think what you really need is to just test the parameter (using IF) to see if it has been populated.

I was afraid of that. I had aspirations of allowing a few different options for naming, which would be trivial if you were able to add the variables and undefined ones just wouldn't print, but when each one has to be tested, it means I would need multiple IF statements to decide between multiple commands. If I wanted to offer just two options, I'd need four copies of the command (XX, XO, OX, OO) and that number grows exponentially with each new option added. sad

Last edited by Rekrul (17 Feb 2020 03:24)

Offline

#4 19 Feb 2020 22:31

Shadow Thief
Member
Registered: 12 Jul 2012
Posts: 175

Re: Is it possible to create an empty variable that's usable?

If you're combining variables, the undefined ones will have no value. You really only run into an issue when you're echoing.

This will display "echo is OFF." because both are unset. You could get around this by using something like echo( instead of echo so that a blank line gets printed.

@echo off
set "one="
set "two="
echo %one%%two%

This will print hello

@echo off
set "one=hello"
set "two="
echo %one%%two%

This will print world

@echo off
set "one="
set "two=world"
echo %one%%two%

This will print helloworld

@echo off
set "one=hello"
set "two=world"
echo %one%%two%

Offline

#5 20 Feb 2020 10:09

jeb
Member
From: Germany
Registered: 19 Nov 2010
Posts: 109

Re: Is it possible to create an empty variable that's usable?

If you really see %prefix% for an undefined variable, then you must be in the command line context, only there the expansion rules can result in such output.
When putting that code into a batch file, undefined variables expands to nothing.

In batch files you could also use default values for empty variables

@echo off 
set "var1=Var has a value"
set "var2="

echo %var1:default%=%
echo %var2:default%=%

Offline

#6 20 Feb 2020 15:50

Rekrul
Member
Registered: 17 Apr 2016
Posts: 69

Re: Is it possible to create an empty variable that's usable?

jeb wrote:

If you really see %prefix% for an undefined variable, then you must be in the command line context, only there the expansion rules can result in such output.

You're right, I was testing it from the command line. I keep forgetting that scripts and the command line behave differently sometimes.

Thank you. smile

Offline

Board footer

Powered by FluxBB