使用批处理检查用户输入是否在txt文件中 [英] Check if user input is in txt file, with batch

查看:59
本文介绍了使用批处理检查用户输入是否在txt文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为局域网联网的计算机批量制作一个聊天风格的系统. 我想检查用户名是否被使用,如果不允许使用,如何检查用户在此行中输入的内容(set/p name2 =) 我已经在测试文件中尝试过了,但是无法正常工作

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

问题在于,即使没有名字,它也总是不起作用.创建了Users.twml文件,如果我手动放置:Yes,则在用户选择名称后将其加载到的位置会将名称保存到文件中.

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.

我想要的是:用户选择名称,它检查是否在文件中(采用),如果是,则尝试使用goto启动重试;如果未采用,则将其写入文件并继续.最好使用goto section命令,以便我可以指定要去的地方...

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

如果正确运行,则是users.twml内部的外观

This is what the inside of users.twml looks if :yes if ran correctly

username1
username2
username3
username4

@@@@@@@@@@@@@这里有新的编辑内容@@@@@@@@@@@@@@@@@@

@@@@@@@@@@ New edits blow here @@@@@@@@@@@@@@@@

所有固定和可运行的文件,我在文本文件中使用了%user2%,在主文件中使用了%name2%,所以难怪他们不能一起工作.虽然还是不懂findstr ...这就是为什么我不愿意在凌晨1点写任何形式的代码:)

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 :)

推荐答案

您混淆了||&&(我也这样做,这就是为什么我从不使用它们的原因)

You've confused || and && (I do too, which is why I never use them)

&&表示如果成功",即errorlevel0. ||表示相反.

&& means "if succeeded" ie, errorlevel is 0. || means the opposite.

findstr将成功". (嗯,请确保您不希望/i-/b /e等同于/x)

findstr will "succeed" if the target string is found. (um, sure you don't want /i - and /b /e is equivalent to /x)

如果将||固定为&&,那么-好吧,也许是found而不是fail ...

If you fix the || to && then - well, perhaps found rather than fail...

但是

您的代码仍将不起作用,因为您在文件中记录的数据将以空格开头.一旦删除了重定向和目标定义(第一个空格除外),echo行中的所有字符都将被重定向到文件.您的代码是

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%

因此,删除重定向字符串后,第一个空格将留下 Space %user2%

so, removing the redirection string and the first space leaves Space%user2%

修复使用情况

>> %twml% echo %user2%

redirection specifier including filename
space
echo
space
%user2%

第一个空格将重定向伏都教与echo指令分开,第二个空格将echo与所需的字符串分开.

The first space separates the redirection voodoo from the echo instruction, and the second separates the echo from the required string.

这是我的测试设置,使用您的原始方法,使用cls,消除了延迟,并使用了对我的系统方便的文件

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

对我来说很好!

这篇关于使用批处理检查用户输入是否在txt文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆