Twml
Twml

Reputation: 441

Batch - check if user input is in a text file then proceed

I am making a chat style system for LAN networked computers, and want to make a check for if username is in use or not. I can't get it to work though, here is what i have in a test file, that Does work...

@echo off
title Testing usercheck

setlocal EnableDelayedExpansion

set "user2="
set /p "user2="
find /c "%user2%" Users.twml
if %errorlevel% equ 1 goto yes
goto nope


:empty
cls
echo empty response, should not be triggered...
pause
goto end


:yes
cls
echo yes, you can use that
>> Users.twml echo %user2%
pause
goto end


:nope
cls
echo nope. that is taken... try again!
pause
goto end


:end
cls
echo.
echo End of testing file...
echo.
echo press any key to exit
pause>nul
exit

This takes user input, checks the file to see if it is in there, if it is, says cant use, if its not there, it says can use and saves the name to the text file so others cant pick same. However when i put it in my main file that im making the same thing doesnt work, when saving the name to file the file records

ECHO is off.

Being caused by this line here

>> Users.twml echo %user2%

and not the username the user entered, Strangely though it works in the test file perfectly, here is the section of my main file that doesnt work.

@echo off
title Batch Chat Room
mode con: cols=83 lines=10

setlocal EnableDelayedExpansion


:startup
cls
echo Pick A UserName
echo 1-16 Character limit.
set "user2="
set /p "name2="
if "!name2!" == "" goto startup
if "!name2!" == " " goto startup
if "!name2!" == "  " goto startup
if "!name2!" == "   " goto startup
if "!name2!" == "    " goto startup
if "!name2!" == "     " goto startup
if "!name2!" == "      " goto startup
if "!name2!" == "       " goto startup
if "!name2!" == "        " goto startup
if "!name2!" == "         " goto startup
if "!name2!" == "          " goto startup
if "!name2!" == "           " goto startup
if "!name2!" == "            " goto startup
if "!name2!" == "             " goto startup
if "!name2!" == "              " goto startup
if "!name2!" == "               " goto startup
if "!name2!" == "                " goto startup
if not "!name2:~16!" == "" goto over
find /c "%user2%" Users.twml
if %errorlevel% equ 1 goto conta
goto tryagaina


:conta  
cls
echo yes, you can use that
>> Users.twml echo %user2%
pause 
:: This pause is just for testing to add a wait here
copy /y NUL Connected.twml >NUL
copy /y NUL Directory.twml >NUL
attrib +h Connected.twml
attrib +h Directory.twml
cls
>> Connected.twml echo [System] %computername%:%username% Has joined as: %name2%
>> Directory.twml echo [System] %name2% Has joined the chat.
title Lightfoot Web - Batch ChatRoom - (User Typing) - (Username:%name2%)
goto A


:tryagaina
goto startup

Here is the inside of Users.twml First 2 lines were written by the testing file, the others by my main file.

twml
bob
ECHO is off.
ECHO is off.
ECHO is off.

At first i tried findstr, but couldn't get it to work, so i used errorlevel as i do know how that works quite well. but it wont save name to file using my main file. Im stumped with this because the lines for Connected.twml and Directory.twml both write their content, just Users.twml will not.

Upvotes: 0

Views: 79

Answers (1)

Twml
Twml

Reputation: 441

Answered!

Im such an Idiot lol I used %name2% in my main file and %user2% in the test file, so of course the parts wont work together, they not same variable. Duh...

Here is the fixed code:

:startup
cls
echo Pick A UserName
echo 1-16 Character limit.
set "name2="
set /p "name2="
if "!name2!" == "" goto startup
if "!name2!" == " " goto startup
if "!name2!" == "  " goto startup
if "!name2!" == "   " goto startup
if "!name2!" == "    " goto startup
if "!name2!" == "     " goto startup
if "!name2!" == "      " goto startup
if "!name2!" == "       " goto startup
if "!name2!" == "        " goto startup
if "!name2!" == "         " goto startup
if "!name2!" == "          " goto startup
if "!name2!" == "           " goto startup
if "!name2!" == "            " goto startup
if "!name2!" == "             " goto startup
if "!name2!" == "              " goto startup
if "!name2!" == "               " goto startup
if "!name2!" == "                " goto startup
if not "!name2:~16!" == "" goto over
find /c "%name2%" Users.twml
if %errorlevel% equ 1 goto conta
goto tryagaina


:conta  
cls
echo yes, you can use that
>> Users.twml echo %name2%
pause 
:: This pause is just for testing to add a wait here
copy /y NUL Connected.twml >NUL
copy /y NUL Directory.twml >NUL
attrib +h Connected.twml
attrib +h Directory.twml
attrib +h Users.twml
cls
>> Connected.twml echo [System] %computername%:%username% Has joined as: %name2%
>> Directory.twml echo [System] %name2% Has joined the chat.
title Batch Chat Room (Username:%name2%)
goto A


:tryagain
goto startup

this now works, case closed :)

Upvotes: 1

Related Questions