user3178828
user3178828

Reputation: 5

How can I have a Bat file find a string and confirm its existence?

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

Can this locate a string/variable?

The %in% =1 may not work. I just want a comand used to locate text in a document.

Upvotes: 0

Views: 94

Answers (2)

mihai_mandis
mihai_mandis

Reputation: 1658

Generally you can use findstr to locate a string in a file. Then you'll need to check the value of %errorlevel% variable to see if string was found or not. %errorlevel% is set to 0 if previous command ran successfully, and not 0 otherwise. I assume: %c% is your script variable, username and password take the whole line in the file.

@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

Upvotes: 1

SachaDee
SachaDee

Reputation: 9545

another way :

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

Upvotes: 0

Related Questions