dimanche 13 août 2017

GOTO in IF Statement Batch Scipt

My batch script compares a username to a list of usernames and if the username variable is in the list of usernames, then start notepad.exe. Else, print no. I am doing this using GOTO.

So if the username is in the list, goto match1 and launch notepad. Else, goto match2 and print no. But it seems even when the username is in the list, it always goes to the else part and prints no. Here's my code:-

@ECHO OFF

set user=username1
set list=username3 username2 username1 

(FOR %%a IN (%list%) DO (if %user%==%%a (GOTO MATCH1 
) else ( GOTO MATCH2 
)                   
))

:MATCH1
notepad.exe

:MATCH2
echo no 

pause  

However, when I make the list this way, it launches notepad and prints no:-

 set list=username1 username2 username3

Please note that when I did this without using GOTO, it worked well. So for example, if there's a match, launch notepad, else, print no. Am I using GOTO in a wrong manner? Or did I misunderstood the functionality of GOTO?

Aucun commentaire:

Enregistrer un commentaire