0
@ECHO OFF
CLS

SET ad =
SET /p ad = Do you want to [A] Allow or [D] Deny? 
ECHO "%ad%"

I type A and press Enter, and the output I get is:

Do you want to [A] Allow or [D] Deny?
""

Why doesn't it work?

2 Answers 2

5

You aren't setting %ad%. You are setting %ad %.

Some languages are more picky about whitespace than others. The cmd.exe script language is one of them – the syntax for setting a variable is set name=value, and if you add spaces around the =, they become part of the variable name and value.

If you're writing the script for Windows 7/8 only, I strongly recommend learning PowerShell instead. The language used by cmd.exe is awful.

11
  • 1
    Note that PowerShell also comes with Vista. It's also available as a download for XP (and apparently as a recommended update, though I can't confirm). Note that there are different versions with varying features, though the basic syntax is the same. It's also far more powerful than the legacy command line (it supports .NET as far as any programming language, e.g. C# or VB.NET), though a bit heavier.
    – Bob
    Feb 10, 2013 at 14:40
  • I didn't even know that variables could have spaces! This is the most ridiculous property of a language I've seen until now. I'm actually just using this to change some properties on a server, so I just want it to work as fast as possible.. Feb 10, 2013 at 14:51
  • @StudentofHogwarts: You still haven't seen some interesting properties of PHP, or MUMPS, or FORTRAN... Feb 10, 2013 at 16:44
  • @grawity Actually PHP was the first language I learned, and you are completely correct, there is a lot of (please pardon me, but this is actually right from the soul) crap in PHP. So I'm actually considering using Django instead, but I suppose it's more "okay" since that's what I've "grown up with".. Feb 10, 2013 at 16:47
  • 1
    @StudentofHogwarts xkcd.com/1171 I've seen regular expressions that were a few pages long.
    – Mark Allen
    Feb 10, 2013 at 21:17
3

The command processor in Windows 8 is unchanged from the one in Windows 7, as far as I know

Everything up to the equals sign is part of the variable name. So in this case, your variable name is actually "ad " without the quotes. You can view current environment variables with the command set without any parameters - this is very useful for debugging.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .