M
Matt Young
Greetings,
Today, I was building a script and came across something I couldn't explain.
If anyone can explain the behavior I'm experiencing I would appreciate it!
Here is the scenario, for each txt file in a directory, I'm using looking
for the string 'error' in it. Look at the code below.
I'm using a FOR to loop through the txt files and the FIND command to search
the files for 'error'.
Here is where I'm at a loss. I expect FIND will return an errorlevel of 0 if
it finds the string 'error' and errorlevel 1 if not for each file. But, in
reality, the errorlevel isn't changing until the FOR loop is exited.
Can anyone explain this behavior?
I found if I replace,
find /I "error" test.txt
with,
find /I "error" test.txt || echo failed
I can test if each file has passed or failed...
Thanks in advance!
Matt Young
-----------------------
@echo off
REM: Test batch one
echo %ERRORLEVEL% at start
REM: create a text file for test
echo asdf>test.txt
for /f %%i in ('dir /b *.txt') do (
echo testing %%i
REM: find should change errorlevel to 1
find /I "error" %%i
echo %ERRORLEVEL% inside for loop
)
echo %ERRORLEVEL% outside for loop
REM: delete the testing text file
del test.txt
-----------------------
output from running the batch on WinXP Pro and Win2000 Pro
0 at start
testing test.txt
---------- TEST.TXT
0 inside for loop
1 outside for loop
Today, I was building a script and came across something I couldn't explain.
If anyone can explain the behavior I'm experiencing I would appreciate it!
Here is the scenario, for each txt file in a directory, I'm using looking
for the string 'error' in it. Look at the code below.
I'm using a FOR to loop through the txt files and the FIND command to search
the files for 'error'.
Here is where I'm at a loss. I expect FIND will return an errorlevel of 0 if
it finds the string 'error' and errorlevel 1 if not for each file. But, in
reality, the errorlevel isn't changing until the FOR loop is exited.
Can anyone explain this behavior?
I found if I replace,
find /I "error" test.txt
with,
find /I "error" test.txt || echo failed
I can test if each file has passed or failed...
Thanks in advance!
Matt Young
-----------------------
@echo off
REM: Test batch one
echo %ERRORLEVEL% at start
REM: create a text file for test
echo asdf>test.txt
for /f %%i in ('dir /b *.txt') do (
echo testing %%i
REM: find should change errorlevel to 1
find /I "error" %%i
echo %ERRORLEVEL% inside for loop
)
echo %ERRORLEVEL% outside for loop
REM: delete the testing text file
del test.txt
-----------------------
output from running the batch on WinXP Pro and Win2000 Pro
0 at start
testing test.txt
---------- TEST.TXT
0 inside for loop
1 outside for loop