问题
I am making a chat style system in batch for LAN networked computers. I want to check if a username is taken or not and if it is not allow it to be picked, How can I check if what the user inputs in this line (set /p name2=) I have tried this in a test file, but cant get it to work
:startup
set "fail="
set "name2="
set /p "name2=Enter Your Username: "
cls
findstr /b /e /l /c:"%name2%" <"Users.twml" >nul || set fail=1
if defined fail (
goto nope
)
:yes
cls
echo yes, you can use that
echo >> Users.twml %name2%
pause
goto endoftest
:nope
cls
echo thats taken try again
ping locahost -n 3 >nul
goto startup
The issues is it always goes to nope, even if name isn't taken. The Users.twml file is created and if i manually put :Yes where it will load after the user picks name it saves the name to file.
What i want is: user picks name, it checks if its in file(taken) if yes retry with a goto startup, if its not taken, write it to the file and continue on. preferably with a goto section command so i can specify where to go...
The code block above is what i need help with ^^^
The stuff below is what i currently use and it works just no name verification.
Here is the block i currently have for use name selection that is working without the name verification
:startup
cls
echo Pick A UserName
echo 1-16 Character limit.
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
REM continues on after name picked if matches above requirements with stuff below
echo >> Connected.twml [System] %computername%:%username% Has joined as: %name2%
echo >> Directory.twml [System] %name2% Has joined the chat.
goto chat
This is what i'm trying to go for to give you a visual idea. And currently is doesn't work
:startup
cls
echo Pick A UserName
echo 1-16 Character limit.
set "fail="
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
REM all the above works, now lets try to check if its taken
findstr /b /e /l /c:"%name2%" <"Users.twml" >nul || set fail=1
if defined fail (
goto nope
)
REM continues on after name picked if matches above requirements with stuff below
echo >> Connected.twml [System] %computername%:%username% Has joined as: %name2%
echo >> Directory.twml [System] %name2% Has joined the chat.
echo >> Users.twml %name2%
goto chat
:nope
cls
echo thats taken try again
ping locahost -n 3 >nul
goto startup
This is what the inside of users.twml looks if :yes if ran correctly
username1
username2
username3
username4
@@@@@@@@@@ New edits blow here @@@@@@@@@@@@@@@@
All fixed and working i used %user2% in text file and %name2% in main file so no wonder they dont work together. still dont understand findstr though... This is why you dont write any form of code at 1am with low sleep i guess :)
回答1:
You've confused ||
and &&
(I do too, which is why I never use them)
&&
means "if succeeded" ie, errorlevel
is 0
. ||
means the opposite.
findstr
will "succeed" if the target string is found. (um, sure you don't want /i
- and /b /e
is equivalent to /x
)
If you fix the ||
to &&
then - well, perhaps found
rather than fail
...
BUT
your code will still not work because the data you are recording in your file will be preceded by a space. Any characters in a echo
line, once the redirection and target definitions are removed other than the first space are redirected to the file. Your code is
echo
space
redirection specifier including filename
space
%user2%
so, removing the redirection string and the first space leaves Space%user2%
to fix use
>> %twml% echo %user2%
ie
redirection specifier including filename
space
echo
space
%user2%
The first space separates the redirection voodoo from the echo
instruction, and the second separates the echo
from the required string.
Here's my test setup, using your original approach, with cls
, delays removed and using a file that's convenient for my system
@ECHO Off
SETLOCAL
:startup
SET "twml=u:\users.twml"
set "fail="
set "user2="
set /p "user2=Enter Your Username: "
REM cls
IF NOT DEFINED user2 GOTO :EOF
findstr /X /l /c:"%user2%" <"%twml%" >nul && set fail=1
if defined fail (
goto nope
)
:yes
REM cls
echo yes, you can use that
>> %twml% echo %user2%
goto :eof
:nope
REM cls
echo thats taken try again
:: ping locahost -n 3 >nul
goto startup
GOTO :EOF
works fine for me!
来源:https://stackoverflow.com/questions/46396536/check-if-user-input-is-in-txt-file-with-batch