Metalhead89
Metalhead89

Reputation: 1920

Windows Batch loop all variables with findstr

In my Windows batch file I have a various amount of variables. Lets say I have the following variables:

set varTest1=test1
set varTest2=test2
set otherVar=variable500
set varS=string
set yetAnotherVar=foo

They do really make no sense buts thats not the point. I am looking for a method that prints out all values of variables that start with var: So when I run my batch with a certain help parameter it should print out all three variables starting with var and its value. The output could look like this:

These are the available variables:
varTest1 : test1
varTest2 : test2
varS : string

I created the following for reading the parameter:

IF "%1" == "" ( 
echo No help parameter was set. Program will exit. ) ELSE ( 
IF "%1" == "help" ( 
call :showAllAvailableVars ) ELSE (
echo Do something else))

Now I would have my method

:showAllAvailableVars

I think the solution could be something with the findstr method but I could not figure it out how to do that because findstr is mainly for files and not for searching through own program variables.

Upvotes: 0

Views: 384

Answers (2)

jeb
jeb

Reputation: 82418

You could use set var to print all variables which begins with var.
See also set /?

Upvotes: 0

RIRAJAT
RIRAJAT

Reputation: 40

Create array instead of different variables. Like,

set var[0]=test1

set var[1]=test2

set var[2]=string

then in your 'showAllAvailableVars' function do this

for /L %%i in (1,1,%n%) do echo !var[%%i]!

Upvotes: 2

Related Questions