Check if user input is in txt file, with batch

爷,独闯天下 提交于 2019-12-06 05:53:30

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!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!