25

In Windows batch files, I accept variable from user input by set /p var1=, after var1 is used, I don't know how to reset/clear its value.

If I don't reset/clear its value, when user meets set /p var1= again, and user enter directly. the previous input value will be still there. I don't want it, How to reset it for new user input?

dbenham
  • 127,446
  • 28
  • 251
  • 390
Henry Leu
  • 2,184
  • 4
  • 22
  • 34

3 Answers3

53

To clear a variable, regardless how it was set:

set "var1="
dbenham
  • 127,446
  • 28
  • 251
  • 390
0

rare issue, but you can try this:

set "var=%var*=%"
Endoro
  • 37,015
  • 8
  • 50
  • 63
0

Different answers to original question were already provided above. One additional hint: within batch-script you can use the scopes:

setlocal
...
endlocal

This ensures that the variables are only accessible within your batch script and not affecting variables outside of your batch script (i.e. your variables are also not accessible outside of the batch script). By exiting of the batch script - the variables used locally get automatically "cleared".

Dr.CKYHC
  • 11
  • 2