Here I made a program in batch that detects all files with the .user extension. Then it allows the user to pick a username by entering the number associated with that username.
The code is messy, so I will explain.
@echo off setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION :login_menu cls set x=1 set users= cd "users" for %%A in (*.user) do ( echo !x!. %%~nA set users=%%~nA:!x!,!users! set /a x=!x!+1 ) echo. set /p ch=Select User: if "%ch%" == "" (goto login_menu) for %%B in (%users%) do ( for /f "tokens=1,2 delims=:" %%C in ("%%B") do ( set userNumber=%%D set userN=%%C if !ch! == %%D goto password echo BDEV: %%B pause ) ) echo. echo That user doesn't exist! pause goto login_menu :password cls cd "users" for /f "tokens=1,2 delims=:" %%E in (!userN!.user) do (set password=%%F) echo Enter your password, !userN! echo. set /p password1=Password: if %password1% == %password% goto menu echo. echo That password is invalid! pause goto password :menu echo Hey! You're logged in as !userN! pause
x is going to be the number which the username will be associated with. The variable
users Makes sort of a 'map' to usernames to the number associated with that username
In the first
for loop, it gets all the files in the folder users. It
echo's out all the usernames that the user can pick.
for loop goes into the variable
users and separates the usernames with their respective numbers. For example: if I have the usernames
admin:4,steve:3,john:2,jane:1 it will separate them into
for loop (which is in the second
for loop) separates each username to number into separate variables. For example, if we have the username Collins with the number 3, it will put the username Collins in the
userN variable and the number 3 into the variable
It then checks what number the user selected.
for loop goes into the user file that the user has selected. So if the user selected
admin it will go to the user file
admin.user and find the password.
If the password is invalid, it rejects access. If it correct it allows access.
Is there any way to make it less like spaghetti code?
If you need more explanation, I will be happy to provide more information.