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?
However, you can clear the value of an environment variable using Command Prompt. To unset an environment variable from Command Prompt, type the command setx variable_name “”.
set /p allows to have the user enter a value and assign it to a environment variable. The new line that echo writes can be piped into set /p to echo text without new lines.
The /P switch allows you to set the value of a variable to a line of input entered by the user. Displays the specified promptString before reading the line of input. The promptString can be empty. When batch file reaches this point (when left blank) it will halt and wait for user input.
The set command is often used in the Autoexec. nt file to set environment variables. If you use the set command without any parameters, the current environment settings are displayed. These settings usually include the COMSPEC and PATH environment variables, which are used to help find programs on disk.
To clear a variable, regardless how it was set:
set "var1="
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With