1

I need a batch file where a user enters a password and if it is correct it maps a drive. If it is wrong then it needs to go back to the start

I have this

:start
echo    Username=****

SET /P password=Please enter the password

IF %password%==password goto map
IF %password%!==!password goto start

:map

NET USE Z: \\server\folder

It works if the password entered is correct, but doesnt if it is wrong

Any help?

3 Answers 3

2

That's because the check

if %password%!==!password

can never be true. if compares two strings to each side of the ==. Ask yourself whether that condition can ever be true ;-)

You probably want

if %password%==password goto map
goto start

instead. The goto map skips the next line, so it won't be executed and if the condition was false it will return to start.

Another option is

if %password%==password (goto map) else (goto start)

which is probably more familiar to programmers accustomed to structured programming.

One word of caution: You should normally quote your strings to be checked to guard against spaces in the input:

if "%password%"=="password" ...
Sign up to request clarification or add additional context in comments.

2 Comments

Simpler yet: if "%password%" neq "password" goto start. No :map label required.
That too, indeed. Doesn't really help me here, though, as the accept went to a later question detailing the exact same content.
0

You don't need IF %password%!==!password goto start altogether. If the password is ok, then goto map, otherwise goto start. That is if you replace IF %password%!==!password goto start with goto start you'll get what you want.

Comments

0
@echo off
color 02
set num1=0
set num2=1 
set terminator=5
:loop
set /a num1= %num1% + %num2%
if %num1%==%terminator% goto close
goto open
:close
echo %num1%
pause 
exit
:open
echo %num1%
goto loop

try this code num1 is the number to be incremented and num2 is the value added to num1 and terminator is the value where the num1 will end you can indicate different value for terminator in this statement (if %num1%==%terminator% goto close) this is the boolean expression goto close is the process if the boolean is true and goto open is the process if the boolean is false you can only understand this if you are a programmer

please vote if you understand this codes

save it in a file name with .bat extension

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.