Batch programming: finding a given string in another program's - maybe delayed - output
I would like to start the Apache service on my Windows 7 with the help of a batch program. That's a really simple task, all I have to do is type this:
net start Apache2.2
then press Enter; BUT I must have admin rights to do so, otherwise I get some error messages like this:
System error 5 has occurred.
Access is denied.
It's OK, but I would like to check the output of the "net start XY" command, and in case the output (or the response) contains the mentioned "Access is denied" string, then I would like to do some other things in another part of the batch program (output some custom error messages and stuff).
I tried to check the output with FIND command like this (IT DOESN'T WORK THE WAY IT SHOULD):@echo off
set search_string=Access is denied
echo We are looking for this string: "%search_string%"
rem set errorlevel =
net start Apache2.2 | find /i "%search_string%" > nul
echo The errorlevel number is: "%ERRORLEVEL%"
if %ERRORLEVEL% EQU 0 goto gotcha
if %ERRORLEVEL% EQU 1 goto not_found
if %ERRORLEVEL% EQU 2 goto para
:gotcha
echo OK, found it
echo So maybe you don't have the rights to do so...
goto end
:not_found
echo String not found...
echo So this would mean there are no problems related to admin-rights... but YES, there are... :-S
goto end
:para
echo Something's not OK...
goto end
:end
echo -- END --
pause
Starting this batch program without admin rights, it outputs the mentioned "Access is denied", so it should jump to "gotcha" label. Unfortunately it doesn't work fine, it jumps to "not_found", which means it didn't find the given string ("Access is denied").
Maybe the problem is that
net send XY
command sends the "Access is denied" output a little bit delayed, just after checking for admin rights, and find doesn't take care about it anymore. But it's just guessing, I don't know the real explanation of the problem.
BUT the code above DOES work when I create another little batch program which just simply echoes the lines above with the "Access is denied" string. So t开发者_运维百科he other little batch program, which shows that 'find' command can work the way it's written like above, looks like this:
@echo off
echo.
echo System error 5 has occurred.
echo.
echo Access is denied. blabla
echo.
I saved this file with the name "write_accessdenied_stuff.bat".
After that, I tried the code above, but instead of the "net send XY...." row I wrote this one:
write_accessdenied_stuff.bat | find /i "%search_string%" > nul
so the beginning of the code is changed like this, and IT WORKS:
@echo off
set search_string=Access is denied
echo We are looking for this string: "%search_string%"
write_accessdenied_stuff.bat | find /i "%search_string%" > nul
..........
..........
With this one, the find command sets the errorlevel to 0, which means it has found the string given, so the execution of the program jumps to "gotcha" label.
So how can I find the little bit delayed "Access is denied" output of the "net start XY" command?
Thank you very much in advance!!!
Use
net start Apache2.2 2>&1 | find /i "%search_string%" > nul
in order to tie STDERR to the standard output.
精彩评论