如何在不使用ERRORLEVEL(批处理)的情况下在文件中查找某个单词

时间:2015-12-15 22:51:53

标签: batch-file findstr errorlevel

我可以使用ERRORLEVEL,但尝试使用循环失败。 我正在写一批“shell”。 自从我尝试过并试过,我终于寻求帮助了。 我不想使用errorlevel的原因是因为循环。

(FULL)SHELL

@set /p build=<"C:\Users\%username%\Desktop\WellOS2\WellOS\Build".txt
@title WellOS V.%build%
@echo off
goto boot
:register
cls
echo You are registering...
echo If this is an error press CTRL + C NOW...
pause
cls
set /p user= Enter your username: 
set /p passwordreg= Enter your password: 
mkdir "C:\Users\%username%\Desktop\WellOS2\Users\%user%"
mkdir "C:\Users\%username%\Desktop\WellOS2\Users\%user%\Documents"
echo %passwordreg% >"C:\Users\%username%\Desktop\WellOS2\Users\%user%\password".txt
echo 2 >"C:\Users\%username%\Desktop\WellOS2\OSfiles\bootset".txt
echo Your done.
pause
goto welloslog
:booterror
echo Sorry the boot file has an error. Check the user manual for BOOT$
pause
:boot
set /p boot=<"C:\Users\%username%\Desktop\WellOS2\OSfiles\bootset".txt
if %boot% == 1 goto register
if %boot% == 2 goto welloslog
goto booterror
cls
:ERROR
cls
echo ----------ERROR-------------------
echo %error%
pause
goto %back%
:welloslog
cls
echo Welcome to WellOS2!
echo ----------------LOGIN-------------
set /p user= Username: 
if exist "C:\Users\%username%\Desktop\WellOS2\Users\%user%" goto pass
set error= Sorry that account doesn't exist.
set back=welloslog
goto welloslogerror
:pass
set /p password=<"C:\Users\%username%\Desktop\WellOS2\Users\%user%\password".txt
set /p passwordlog= Password: 
if /i %passwordlog% == %password% goto wellos
set error= Sorry! wrong password.
set back= welloslog
goto error
:wellos
cls
:wellosnocls
echo --------------MAIN---------------
echo type help for help
set /p command= #: 
if exist "C:\Users\%username%\Desktop\WellOS2\Programdata\%command%.sys" set type=sys
if exist "C:\Users\%username%\Desktop\WellOS2\Programdata\%command%.pro" set type=pro
if exist "C:\Users\%username%\Desktop\WellOS2\Programdata\%command%.sys" goto po
if exist "C:\Users\%username%\Desktop\WellOS2\Programdata\%command%.pro" goto po
set error= !Unreconized program/system program!
set back=wellos
goto error
:po
set lines=0
echo --------------%command%.%type%---------------
:porep
set /a lines=%lines% + 1
set /p "code="<"C:\Users\%username%\Desktop\WellOS2\Programdata\%command%.%type%\%command%.%type%-%lines%".wellcode
if "%code%"=="GOWELL" goto wellosnocls
findstr /I /L "if" "C:\Users\%username%\Desktop\WellOS2\Programdata\%command%.%type%\%command%.%type%-%lines%.wellcode"
:skip
call %code%
goto porep
::Tools
:iftl
%code%
goto porep

计划开放(我正在谈论的内容,并且遇到问题......)

:po
set lines=0
echo --------------%command%.%type%---------------
:porep
set /a lines=%lines% + 1
set /p "code="<"C:\Users\%username%\Desktop\WellOS2\Programdata\%command%.%type%\%command%.%type%-%lines%".wellcode
if "%code%"=="GOWELL" goto wellosnocls
findstr /I /L "if" "C:\Users\%username%\Desktop\WellOS2\Programdata\%command%.%type%\%command%.%type%-%lines%.wellcode" goto iftl
:skip
call %code%
goto porep
::Tools
:iftl
%code%
goto porep

2 个答案:

答案 0 :(得分:0)

findstr "targetstring" datafilename >flagfilename
for %%a in (flagfilename) do if %%~za==0 echo not found
for %%a in (flagfilename) do if %%~za neq 0 echo found
除此之外,你的问题太模糊了。

答案 1 :(得分:0)

以下命令返回包含单词textfile.txt的文本文件word的所有行(如果您希望搜索区分大小写,请删除/I开关):

findstr /I /L "word" "textfile.txt"

使用for /F,您可以捕获输出并测试它是否为空,因为如果没有遇到匹配,循环不会迭代:

set "FOUND="
for /F "delims=" %%F in ('
    findstr /I /L "word" "textfile.txt"
') do (
    set "FOUND=Yes"
)
if defined FOUND (
    echo One or more matches found.
    rem do something...
) else (
    echo No match found.
    rem do something else...
)

在命令提示符下键入for /?if /?以获取有关已使用命令的详细信息。

还有一种隐式使用ErrorLevel的方法,这意味着您无需通过%ErrorLevel%!ErrorLevel!if ErrorLevel之类的内容查询其值,即使用时条件命令分隔符:

  • &&分隔符仅在前一个命令成功的情况下执行以下命令,即它返回ErrorLevel 0; (findstr会在遇到匹配时返回0;)
  • ||分隔符仅在前一个命令失败的情况下执行以下命令,即返回ErrorLevel以外的0; (findstr如果未遇到匹配,则返回非零ErrorLevel;)

以下代码行演示了用法:

findstr /I /L "word" "textfile.txt" && (echo One or more matches found.) || echo (No match found.)