Reputation: 515
I am using SET in cmd to set a new environment variable, and immediately use it afterwards for comparison. The user is prompted to input a string, and afterwards it should check if the input matches another string (basically a password).
set /P pwd=pwd: & if %pwd%==pwd (echo yes) else (echo no)
The problem I'm having is that it appears that the variable is not set, because no matter if I input the password or not, it always returns me No.
I have also tried separating both commands in a .bat file, but it wouldn't work either.
Upvotes: 2
Views: 3050
Reputation: 274
Your code doesn't work because it's on one line. See set /?
, setlocal /?
, for /?
and cmd /?
to see a discussion on delayed expansion.
CMD processes batch files line by line and fills in all %var%
when it reads the line. This is how MSDos worked and CMD needs to be compatable. Delayed expansion the variables are expanded when they are used not when read. As !var!
is a legal MSDos name (as in %!var!%
) you must optin to new behaviour (new in 1993 anyway).
Upvotes: 2