Resetting multiple variables at once

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
batchcc
Posts: 139
Joined: 17 Aug 2015 06:05
Contact:

Resetting multiple variables at once

#1 Post by batchcc » 09 Nov 2016 14:57

I have a batch file with a large amount of variables now rather than adding this to the beginning of the file is there an easy way to resset all the variables?

Code: Select all

set var1=
set var2=
set var3=
set var4=
set var5=
set var6=
set var7=
set var8=
...

Thank you!

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Resetting multiple variables at once

#2 Post by aGerman » 09 Nov 2016 16:10

You have to undefine them separately but you could use loops.

Code: Select all

for /l %%i in (1 1 8) do set "var%%i="

or

Code: Select all

for /f "delims==" %%i in ('set var') do set "%%i="

or

Code: Select all

for %%i in (a_variable another_variable another_different_variable) do set "%%i="

Steffen

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Resetting multiple variables at once

#3 Post by dbenham » 09 Nov 2016 16:26

The only time you don't need individual SET statements is when you are initializing a series of numeric values. For example, you could initialize four variables to 0:

Code: Select all

set /a var1=var2=var3=var4=0



Dave Benham

batchcc
Posts: 139
Joined: 17 Aug 2015 06:05
Contact:

Re: Resetting multiple variables at once

#4 Post by batchcc » 09 Nov 2016 18:07

Thanks :D

Sponge Belly
Posts: 231
Joined: 01 Oct 2012 13:32
Location: Ireland
Contact:

Re: Resetting multiple variables at once

#5 Post by Sponge Belly » 10 Nov 2016 04:29

Hello All! :)

Check out this technique developed by Aacini.

- SB

Post Reply