批处理 - 检查用户输入是否在文本文件中然后继续

时间:2017-09-25 13:01:17

标签: batch-file find errorlevel

我正在为局域网联网计算机制作一个聊天方式系统,并想检查用户名是否正在使用。我不能让它工作,这是我在测试文件中的内容,它有效...

@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

这需要用户输入,检查文件是否在那里,如果是,不能使用,如果不存在,它说可以使用并将名称保存到文本文件,以便其他人不能选择相同的文件。但是,当我把它放在我的主文件中,我做同样的事情不起作用,当保存名称文件记录文件

ECHO is off.

由此线引起的

>> Users.twml echo %user2%

而不是用户输入的用户名,奇怪的是,尽管它在测试文件中完美地工作,但这是我的主文件中不起作用的部分。

@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

这是Users.twml的内部。前两行由测试文件写入,其他行由我的主文件写入。

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

起初我尝试过findstr,但无法让它工作,所以我使用了errorlevel,因为我知道它是如何工作的。但它不会使用我的主文件将名称保存到文件。我很难过,因为Connected.twml和Directory.twml的行都写了他们的内容,只有Users.twml不会。

1 个答案:

答案 0 :(得分:0)

回答!

我真是个白痴哈哈 我在我的主文件中使用了%name2%,在测试文件中使用了%user2%,所以当然这些部分不能一起工作,它们不是同一个变量。咄...

这是固定代码:

: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

这现在有效,案件结束:)