# Strange Problem with DOS Find Command



## rsanders (Nov 29, 2004)

I am using the DOS "FIND" Command in a Multiple Step Batch File to determine whether the word "Error" appears in a Log file created by the execution of a process.

This is run on Windows2000. 

I want to check the %ERRORLEVEL% returned. The doc I have seen says If the string is found, 0 is returned, if the string is not found, 1 is returned.

My testing has verified the info above.

However, these values are returned properly only when I check the Log file after the multi-step process completes, or check a file that already existed before the batch file was executed.

If I try to issue the FIND command as the next step in a multi-step batch file, the %ERRORLEVEL% returned is always 0.

It is as if the command does not work on files created in the .bat process. 

Does anyone know why this is happening and/or what I can do to correct the problem?

Here is an example of the Command I am executing...

find "Error" logfile.txt
if %ERRORLEVEL% EQU 0 set textfound=Y
if %ERRORLEVEL% EQU 1 set textfound=N
if %ERRORLEVEL% EQU 2 set textfound=E

If I run the above code, and "Error" appears in the the file, the textfound variable is set to Y because ERRORLEVEL returns the value 0.

If I run the above code, and "Error" does not appear in the the file, the textfound variable is set to N because ERRORLEVEL returns the value 1.

If I run the above code, and the file does not exist, the textfound variable is set to E because ERRORLEVEL returns the value 2.

HOWEVER, if I run the above code in a batch script, and the log file was just created in a prior step within the batch script, AND the word "Error" does not appear in the text of the file, the textfound variable is set to Y because, for some unknown reason, ERRORLEVEL returns the value 0.


----------



## Shadow2531 (Apr 30, 2001)

Does it do it if you do the following?

echo bob > logfile.txt
find "Error" logfile.txt
if %ERRORLEVEL% EQU 0 set textfound=Y
if %ERRORLEVEL% EQU 1 set textfound=N
if %ERRORLEVEL% EQU 2 set textfound=E
echo %textfound%


----------



## rsanders (Nov 29, 2004)

Shadow2531 ... Thanks for the suggestion 

When I pipe data to a log file, everything works as it should...

echo bob > logfile.txt
find "Error" logfile.txt
if %ERRORLEVEL% EQU 0 set textfound=Y
if %ERRORLEVEL% EQU 1 set textfound=N
if %ERRORLEVEL% EQU 2 set textfound=E
echo %textfound%
@pause

Comes back with a 1, meaning it did not find the word

Okay...so that means a file created by DOS does not exhibit the problem.

But I have tried copying the log file to another file via the DOS Copy commnad
and that displayed the same problem.

Can I pipe a file to another file somehow, then try your "excellent" test.


----------



## Shadow2531 (Apr 30, 2001)

So you are saying that if you do

copy this.txt logfile.txt

that it will cause the problem?

The copy command is probably making the errorlevel variable = 0, so maybe you have to reset the variable after the copy command.

Does this work?

copy this.txt logfile.txt
set ERRORLEVEL=
find "Error" logfile.txt
if %ERRORLEVEL% EQU 0 set textfound=Y
if %ERRORLEVEL% EQU 1 set textfound=N
if %ERRORLEVEL% EQU 2 set textfound=E
echo %textfound%
@pause


----------



## rsanders (Nov 29, 2004)

Thanks for the info Shadow2531.

I seemed to have found the real problem.

The real problem seems to be associated with the fact i have to execute the Find command by using the START Command.

The reason: The Batch Script File will be executed and monitored by a scheduling software product called ZEKE. I found out they do not actually start the script under the Windows CMD "umbrella". As a result, External DOS commands cannot be referenced without the START Command.

So ... when I run the Find command wothout the START command in front of it, all works as expected.

However, when the Find command is prefixed with "START" or "START CMD", the errorlevel returned is always 0.

So, I tried a few of the parameters for the command, and found that if I issue a "START /WAIT" command before the FIND, all works as expected. However, if I issue a "START CMD /WAIT" command before the FIND, errorlevel returned is always 0. 

I am having my scheduling group run the script with the "START /WAIT" prefix to see if all will run OK under the ZEKE software.


----------



## Shadow2531 (Apr 30, 2001)

I see. Let us know how that works out.


----------



## rsanders (Nov 29, 2004)

I have completed testing of the Script Run under control of the scheduling Software ZEKE (an ASG Product).

Testing has verified the problem reported only occurrs when I use the "START", "START CMD", or "START CMD /C" command prior to issuing the Find command.

When the Find command is issued with a "START /WAIT" command, all works as expected.


----------



## Shadow2531 (Apr 30, 2001)

Thanks, I'm sure that will help someone who reads this.


----------

