1.

Solve : CMD file using SET?

Answer»

I have a strange problem doing something that should be very simple. I'm running WINDOWS 7 Enterprise, no service pack installed.

I moved the code I was having problems with to the snippet below:


@echo off
SET var1 = this is a test
echo The variable is "%var1%"
set /p vinput = Enter a value
echo The input was "%vinput%"
echo The current variables that start with a v are:
echo set v


When I execute the batch file, I enter 'test' as the RESPONSE to the prompt.


C:\>test
The variable is ""
Enter a value test
The input was ""
The current variables that start with a v are:
set v


NOTICE that neither variable is set. However, if I issue 'SET V' from the command line, the variables are actually set with the correct values.


C:\>set v
var1 = this is a test
vinput =test


Any ideas on why this is not working? This is actually part of a more interesting batch file, but the variable setting is the only part I'm having problems with.Your batch script is working correctly, however it is not doing what you expect. You need to note some points of syntax.

1. Unlike some other languages, in batch scripting, white space (sometimes!) matters.

In a simple SET statement the syntax is set VariableName=VariableValue. Everything before the = sign is considered to be the variable name, and everything after the = sign (up to the last non white space character) is considered to be the variable value.

THUS this line

SET var1 = this is a test

creates a variable called %var1 % (notice the trailing space) which has the value " this is a test"

2. The situation is slightly more complicated with SET /P

Everything before the = sign is considered part of the variable name, but white space immediately after the = sign is ignored.

Thus this line

set /p vinput = Enter a value

creates a variable called %vinput % with a value of whatever the user types at the prompt.

Thus your commands echo %var1% and echo %vinput% show (correctly) null values since those variables are undefined.

This behaviour is unlike (for example) most dialects of BASIC where

VAR = value
var=value
var = value

all create a variable called "var" with a value of "value"

(I introduce a SET /P hint at this point. If you want to put white space after the user prompt so the user input does not immediately follow the prompt (it looks better) then you can use quotes like this

set /p var="Enter a value "
set /p var="What is your name? "
set /p var="Enter a value > "

The quotes are not shown on the screen.)

3. Finally if you want to see all variables with a name starting with the character v then the command

echo set v

is not the one to use, this will merely echo the text "set v". You just use the SET command followed by the initial letter(s) of the variables you are interested in.

eg

set v
set va
set vi



Excellent! Definitely not used to having to worry about a blank between the variable name and the '=' sign, most of my coding is in C.

Thanks much, that fixed the problem.



Discussion

No Comment Found