Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

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

May
855
0
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
 
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
 
It looks like you can use redirection also to redirect stderr.

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

-Scott
 
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_"
 
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.
 

Similar threads

Back
Top