0

I'm trying to make Windows prompt set a default value to a variable as a result of if operator.

I took the following code as a model:

(echo "getter" | find "ette") >nul && set "err=1" || set "err=0" & echo %err%
:: Outputs 1 after calling repeatedly, which shows it found ette substring
(echo "getter" | find "gh") >nul && set "err=1" || set "err=0" & echo %err%
:: Outputs 0 after calling repeatedly, which shows it failed to find gh substring

The above code works perfectly, showing some output each time it is called despite the fact the substring which is looked for has not been found. So I want to make the following code print output:

if %appdata% == %^appdata% (echo theVarUndefined) | find "theVarUndefined" >nul && set "err=1" || set "err=0" & echo %err%

However, it prints nothing!

It works only for undefined environment variables:

if %undefinedVar% == %^undefinedVar% (echo theVarUndefined) | find "theVarUndefined" >nul && set "err=1" || set "err=0" & echo %err%

The above code prints 1.

The question: how can I make if %appdata% == %^appdata% (echo theVarUndefined) | find "theVarUndefined" >nul && set "err=1" || set "err=0" & echo %err% print default value in case the find command didn't find anything?

bimjhi
  • 311
  • 2
  • 11
  • 1
    Unless you apply [delayed variable expansion](https://ss64.com/nt/delayedexpansion.html) (like `echo !err!`), `echo %err%` will return the value of `err` present when the whole command line is parsed. Anyway, what do you want the condition `if %appdata% == %^appdata%` to do? – aschipfl May 18 '21 at 20:38
  • 4
    That looks like a weird workaround for `if defined var (echo yes) else (echo no)` – Stephan May 18 '21 at 20:43

0 Answers0