Batch file 如何让Bat文件找到字符串并确认其存在? 这可以定位字符串/变量吗?

Batch file 如何让Bat文件找到字符串并确认其存在? 这可以定位字符串/变量吗?,batch-file,Batch File,%=1中的%可能不起作用。 我只需要一个comand来定位文档中的文本。通常,您可以使用findstr来定位文件中的字符串。然后需要检查%errorlevel%变量的值,以查看是否找到字符串。 %如果上一个命令成功运行,则errorlevel%设置为0,否则不设置为0。 我假设: %c%是您的脚本变量, 用户名和密码占据文件中的整行 if %c%==Yes ( set /p in= Enter user and password: findstr /m %in% accounts.txt

%=1中的%可能不起作用。
我只需要一个comand来定位文档中的文本。

通常,您可以使用findstr来定位文件中的字符串。然后需要检查%errorlevel%变量的值,以查看是否找到字符串。 %如果上一个命令成功运行,则errorlevel%设置为0,否则不设置为0。 我假设: %c%是您的脚本变量, 用户名和密码占据文件中的整行

if %c%==Yes (
set /p in= Enter user and password:
findstr /m %in% accounts.txt     )
if %in% =0 (
echo There is no account!
)
if %in% =1 (
echo account %in% Found!
)
另一种方式:

@echo off

set c=Yes
set error=1

if "%c%" NEQ "Yes" goto end

set /p in=Enter user and password separated by space: 
findstr /L /X /C:"%in%" accounts.txt > nul

if "%errorlevel%"=="0" (
    echo Account %in% Found!
) else (
    echo There is no account!
)

:end

你有把用户名和密码放在一起的格式吗?pasword可以包含空格…用户名和密码应该在同一行吗?您的代码在这一点上不清楚。这可以在accounts.txt文件的不同行中找到用户名和密码。我认为OP希望在同一行中找到对,因此这将给出错误的结果。我想应该由OP来澄清到底是不是这样。
@echo off&cls
setlocal EnableDelayedExpansion
set $sw=0
set /p User=Enter username : 
set /p Pwd=Enter Password : 

for %%a in (%user% %pwd%) do findstr /i "%%a" accounts.txt && set /a $sw+=1
if !$sw! Equ 2 (echo pass and username OK) else (echo KO)