-1

I can use ERRORLEVEL, but tried and with a loop it failed. I am writing a batch "shell." Since I have tried and tried, I am finally asking for help. The reason I don't want to use errorlevel is because the loop.

(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

PROGRAM OPENER (What I am talking about, and having problems with...)

: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
Expto
  • 53
  • 2
  • 12
  • Why should `ErrorLevel` not be possible? `find`/`findstr` (re-)set it if a/no match is found, so whan querying immediately afterwards, it will give the result; if you query `ErrorLevel` in a loop/block, use [delayed expansion](http://ss64.com/nt/delayedexpansion.html) and use `!ErrorLevel!` rather than `%ErrorLevel%`... alternatively check whether or not the output of `find`/`findstr` is (not) empty, by parsing it with `for /F`, for instance... – aschipfl Dec 16 '15 at 01:31
  • Almost anything you run is going to set `ERRORLEVEL`, even if it just sets it to 0 to indicate success. If you must preserve it, then your best bet is to save it to a variable, run your commands, and then restore it. See http://stackoverflow.com/questions/6498460/batch-programming-error-handling-and-start-command/6527436#6527436 – Ryan Bemrose Dec 16 '15 at 02:11
  • I'm willing to bet that you were using `%errorlevel%` inside of a loop when you should have been using `!errorlevel!`. – SomethingDark Dec 16 '15 at 03:41
  • 1
    You having problems with your code, but where and what type of problems? From your _detailed_ description, my best answer is that there must be a bug in your code – jeb Dec 16 '15 at 08:59
  • By the line `findstr /I /L "if" "C:\*\*.wellcode" goto iftl` in the _program opener_, you want to do `goto iftl` in case a match is found? if so, just place `&&` before `goto iftl`; see also my updated [answer](http://stackoverflow.com/a/34302735/5047996)... – aschipfl Dec 16 '15 at 09:55

2 Answers2

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

beyond that, your question is too vague.

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • I don't get the `datafilename >flagfilename` part of the first line. – Expto Dec 15 '15 at 23:39
  • I have no idea what your datafile name is. You'd need to substitute it in here. You can use any name as a flagfile. This will be a file that will be created with a size of 0 or non-zero. – Magoo Dec 15 '15 at 23:41
0

The following command returns all lines of a text file textfile.txt that contain the word word (remove the /I switch if you want the search to be case-sensitive):

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

With for /F you can capture the output and test whether it is empty, as the loop does not iterate if no match is encountered:

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

Type for /? and if /? in command prompt to get details about the used commands.


There is also a way to use ErrorLevel implicitly, meaning you do not have to query its value by something like %ErrorLevel%, !ErrorLevel! or if ErrorLevel, namely when using conditional command separators:

  • the && separator executes the following command only in case the previous one succeeded, that is, it returned an ErrorLevel of 0; (findstr returns 0 in case a match is encountered;)
  • the || separator executes the following command only in case the previous one failed, that is, it returned an ErrorLevel other than 0; (findstr returns a non-zero ErrorLevel in case no match is encountered;)

The following line of code demonstrates the usage:

findstr /I /L "word" "textfile.txt" && (echo One or more matches found.) || echo (No match found.)
aschipfl
  • 33,626
  • 12
  • 54
  • 99