Link to home
Start Free TrialLog in
Avatar of paulie99
paulie99

asked on

search wildcard

Hi

I have the following script,  but I need to change the pattern from:
set PATTERN=ERROR.=.[1-9]

to

set PATTERN=.[1-9] error(s)

but the above syntax does not work, what is the correct syntax. For having a wildcard number before space then the word error(s) (the number cannot be 0)

@echo off
:: global parameters, please adapt as needed
set FOLDER=C:\test_search
set PATTERN=ERROR.=.[1-9]

set FOUND=0
pushd %FOLDER%
for /f "delims=" %%a in ('dir /b *.* ^|findstr /v "1."') do call :increment "%%a"
popd

echo Found occurences in %FOUND% files. Sending message...
if %FOUND% GEQ 1 (
  net send pc-000024 test-BAD
) else (
  net send pc-000024 test-GOOD
)
echo Message sent.
goto end

:increment
findstr /R "%PATTERN%" %1
if ERRORLEVEL 1 goto end
set /A FOUND=%FOUND%+1

:end
Avatar of brianadkins
brianadkins
Flag of United States of America image


.[1-9] error(s)

is a "regular expression" that means literally:

   any single character
   followed by a single digit number between 1 and 9
   followed by a space character
   followed by the word "error(s)"
   followed by a space character   (you have a trailing space above)

But, the command line for FINDSTR treats spaces as OR unless you tell it to take you literally... so it is looking for this:

   any single character
   followed by a number between 1 and 9  
       OR
   the word error(s)

So try this to get better results:

set PATTERN=.[1-9] error(s)
findstr /R /C:"%PATTERN%" %1

You may also want to get rid of the leading period in your pattern... this means that there must be a character (any character) BEFORE the number or you won't get a match.

Here is a great book on regular expressions, although you can find a lot out on the web...
http://www.amazon.com/exec/obidos/tg/detail/-/0596002890/qid=1068471988/sr=8-1/ref=sr_8_1/002-1835265-5292003?v=glance&n=507846

-Brian Adkins

Avatar of paulie99
paulie99

ASKER

Hi

Thanks for the feedback Brian, I have tried your example but with no joy. to repeat my question:

I have the following script,  but I need to change the pattern from:
set PATTERN=ERROR.=.[1-9]

to

set PATTERN=.[1-9] error(s)

but the above syntax does not work, what is the correct syntax. For having a wildcard number before space then the word error(s) (the number cannot be 0)



@echo off
:: global parameters, please adapt as needed
set FOLDER=C:\search_test\testUDebug2.txt
set PATTERN=.[1-9] error(s)
set FOUND=0
pushd %FOLDER%
for /f "delims=" %%a in ('dir /b *.* ^|findstr /v "1."') do call :increment "%%a"
popd

echo Found occurences in %FOUND% files. Sending message...
if %FOUND% GEQ 1 (
  net send pc-000024 test-BAD
) else (
  net send pc-000024 test-GOOD
)
echo Message sent.
goto end

:increment
findstr /R /C:"%PATTERN%" %1
if ERRORLEVEL 1 goto end
set /A FOUND=%FOUND%+1

:end

Simply repeating the question will not increase the odds of a helpful answer...

I would suggest posting the actual contents of the file you are trying to query.

-Brian Adkins
Hi

I'm using the above script to search a file which will contain the  text similar to the following:
--------------------Configuration: Aerotech - Win32 Debug--------------------
Compiling...
Aerotech.cpp
CAerotech.cpp
MachiningStage.cpp
U500Controller.cpp
Generating Code...
Linking...
   Creating library Debug/Aerotech.lib and object Debug/Aerotech.exp
Performing registration

Aerotech.dll - 0 error(s), 0 warning(s)

It is the above line which I'm concerned with as I want to able to have my script to search file and define whether this line contains:
0 erros(s)

or

9 error(s)

For if it contains 1 or more error(s) then the result should be a netsend of test-bad. If it is zero then the netsend message should be test-ggod.

This works here....  

I changed the pattern to get rid of the "(s)"  ... not needed
and the leading period.

:::::::::::::::::::::::::::::::
@echo off
:: global parameters, please adapt as needed
set FOLDER=C:\test_search
set PATTERN=[1-9].error

set FOUND=0
pushd %FOLDER%
for /f "delims=" %%a in ('dir /b *.* ^|findstr /v "1."') do call :increment "%%a"
popd

echo Found occurences in %FOUND% files. Sending message...
if %FOUND% GEQ 1 (
  net send pc-000024 test-BAD
) else (
  net send pc-000024 test-GOOD
)
echo Message sent.
goto end

:increment
findstr /R "%PATTERN%" %1
if ERRORLEVEL 1 goto end
set /A FOUND=%FOUND%+1

:end
:::::::::::::::::::::::::::::::

-Brian
SOLUTION
Avatar of _nn_
_nn_

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi

I'm operating on an NT environment, and this script just returns a netsend test-good message no matter what value is put before the word error????


Have you run it without "@echo off" to see what is actually happening behind the scenes?

-Brian
The /C:"" option for findstr should work ok on NT too. But in case of, I'd try to "tune" Brian's last idea like this :

@echo off
:: global parameters, please adapt as needed
set FOLDER=C:\search_test
set MASK=*.*
:: or do you rather mean  ?
:: set MASK=testUDebug2.txt
:: Following would misinterpret "10 errors"
:: set PATTERN=[1-9].error
set PATTERN=[^0-9][1-9][0-9]*.error(s)
set FOUND=0
pushd %FOLDER%
for /f "delims=" %%a in ('dir /b %MASK% ^|findstr /v "1."') do call :increment "%%a"
popd

echo Found occurences in %FOUND% files. Sending message...
if %FOUND% GEQ 1 (
  net send pc-000024 test-BAD
) else (
  net send pc-000024 test-GOOD
)
echo Message sent.
goto end

:increment
findstr /R "%PATTERN%" %1
if ERRORLEVEL 1 goto end
set /A FOUND=%FOUND%+1

:end

Hi

I took the above script and applied it to the log file:
--------------------Configuration: Aerotech - Win32 Debug--------------------
Compiling...
Aerotech.cpp
CAerotech.cpp
MachiningStage.cpp
U500Controller.cpp
Generating Code...
Linking...
   Creating library Debug/Aerotech.lib and object Debug/Aerotech.exp
Performing registration

Aerotech.dll - 9 error(s), 0 warning(s)

But got the net send message of test-good back no matter what value I put before the word error(s)
?????
When I specified the txt file in one ogf the above scripts it was to simply the situation to search only one specified file.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi _nn_

as always you come up trumps.

I will spilt the points in your favour with Brian.