SET command versus the cmd window input buffer

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Gimson
Posts: 3
Joined: 29 Dec 2008 06:01

SET command versus the cmd window input buffer

#1 Post by Gimson » 29 Dec 2008 06:21

Hi all !

I have a batch script that has certain variables used as parameters. I predefine the variables at batch file startup and then ask the user whether he would like to change the variables (default / user settings). If the values are to be left to default, the user is supposed to just press ENTER.

I check this via the command

Code: Select all

set /p name_of_variable =


There is one issue I do not quite understand. Running the batch script with default settings right after starting the command window (which means pressing the ENTER key like 5 times in a row) works fine. However if I run the script once, then set some of these variables to say random strings like 'asddaad', after which I launch the batch file again and THEN press ENTER (so to use defaults this time) the output values are still set to the ones from the first launch of the bat file - as in somehow the SET command ignores pressing ENTER and values assigned in this case are somehow taken from some input buffer of the command window (just my hunch, but it seems to work).
Could anybody explain how does that actually work and how to either add something to make my batch file work properly or change the batch file structure so to still have the logic preserved (namely to first set some variables to certain default values and then somehow allow the user to overwrite the ones he wants to).
I'm more or less a beginner to dos batch scripting, so please no complicated replies ;)

Cheers !

carlitos.dll
Posts: 11
Joined: 02 Jul 2008 07:42

#2 Post by carlitos.dll » 30 Dec 2008 18:07

The author has been removed this message.
Last edited by carlitos.dll on 30 Sep 2011 22:43, edited 1 time in total.

BatchFileCommand
Posts: 3
Joined: 30 Dec 2008 16:50
Location: Minnesota

#3 Post by BatchFileCommand » 30 Dec 2008 18:21

Or you could have the predefined variables in one section and the default ones in the other. So here's what it would look like :

:section1
set variable=%input%

:section2
set variable=C:/Users/Username/Desktop
[/code]

But that's just my guess on how you do it. But carlitos probably has the right way to do it.

DosItHelp
Expert
Posts: 239
Joined: 18 Feb 2006 19:54

Re: SET command versus the cmd window input buffer

#4 Post by DosItHelp » 11 Jan 2009 21:32

Gimson,

Gimson wrote:... namely to first set some variables to certain default values and then somehow allow the user to overwrite the ones he wants to ...

... that's the way to go.

The behavior you observe is explained by two batch facts:
1. Variables changes within the batch are still visible when returning back to the command prompt that launched the batch, they are not undone automagically. These variables carry over into a second run of the batch file (as you observed).
2. The "set /p" leaves the variable unchanged if no new string was entered, i.e. just hitting enter when prompted. So the "default" is really "unchanged" and not empty string as one might think.

There are two solutions you can try.
1. Set variable to the default value before executing "set /p" to ask for a new value (as you suggested). This leaves no room for guessing what the variable will be:

Code: Select all

set "name_of_variable=DEFAULT VALUE"
set /p "name_of_variable=Enter new value [default is %name_of_variable%]: "

Even if 'empty string' is desired default:

Code: Select all

set "name_of_variable="
set /p "name_of_variable=Enter new value [default is %name_of_variable%]: "



2. Use the SETLOCAL at the beginning of your batch. This ensures that variable within your batch are locale to your batch file. However you can never be sure how the variables are set when your batch starts. Someone could preset your variables using global environment variables settings.

Code: Select all

@ECHO OFF
SETLOCAL
...
set /p "name_of_variable=Enter new value [default is %name_of_variable%]"



Also note that the extra space in the variable name matters in DOS. The following script sets and echoes two different variables. You may want to avoid this extra space wherever possible:

Code: Select all

set "name_of_variable=value 1"
set "name_of_variable =value 2"
echo.%name_of_variable%
echo.%name_of_variable %



DosItHelp? :wink:

Post Reply