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?

3 Answers 3

53

To clear a variable, regardless how it was set:

set "var1="
0
0

rare issue, but you can try this:

set "var=%var*=%"
1
  • 6
    What is the issue / Why the obfuscation? That simply expands to set "var="
    – dbenham
    Jun 15, 2013 at 1:03
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".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.