How to? Determine a whether vars beginning with certain characters exist...

May 24, 2010
855
0
Northlake, Il
Here's the code:
Code:
Do VarA In /P (Set DS_*) (
   DoSomeThingWithThatVariable %VarA
)
Works fine if variables whose names begin with "DS_" exist, produces "TCC: Z:\TestVars.btm [11] Not in environment "DS_*"" if not. While I will fully admit that this is not a serious problem, I really don't like to write batch files (or programs of any nature, for that matter) that produce error messages in "normal", totally "expected" circumstances.

So the question is simple: is there any way I can determine if any variables whose names begin with "DS_" exist before executing the loop?

- Dan
 

samintz

Scott Mintz
May 20, 2008
1,370
12
Solon, OH, USA
In the few minutes I toyed with this I couldn't figure out how to get a case insensitive search to work. But other than that this works:

Code:
do VarA in /P (set | ffind /vkm /e"^[dD][sS]_") (echo %VarA)
I was looking for a switch that would turn the case sensitivity off so I could use "^DS_" or "^ds_" and not have to be concerned with the case of the environment variable.

-Scott
 

samintz

Scott Mintz
May 20, 2008
1,370
12
Solon, OH, USA
It looks like you can use redirection also to redirect stderr.

Code:
do VarA in /P (set DS_* >&> nul) (echo %VarA)
-Scott
 

Charles Dye

Super Moderator
Staff member
May 20, 2008
3,968
53
Albuquerque, NM
prospero.unm.edu
In the few minutes I toyed with this I couldn't figure out how to get a case insensitive search to work. But other than that this works:

Code:
do VarA in /P (set | ffind /vkm /e"^[dD][sS]_") (echo %VarA)
I was looking for a switch that would turn the case sensitivity off so I could use "^DS_" or "^ds_" and not have to be concerned with the case of the environment variable.
It looks like (?i) makes it case-insensitive:

Code:
set | ffind /v /k /m /e"(?i)^ds_"
 

samintz

Scott Mintz
May 20, 2008
1,370
12
Solon, OH, USA
I skipped right over that section in help at least three times. That wasn't obvious to me.

7. Extended groups

(?#...) comment​

(?imx-imx) option on/off​
i: ignore case
m: multi-line (dot(.) match newline)
x: extended form

Thanks.