Solved

search wildcard

Posted on 2003-11-10
12
1,598 Views
Last Modified: 2008-02-01
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
0
Comment
Question by:paulie99
  • 5
  • 4
  • 3
12 Comments
 
LVL 5

Expert Comment

by:brianadkins
ID: 9714883

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

0
 

Author Comment

by:paulie99
ID: 9715077
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
0
 
LVL 5

Expert Comment

by:brianadkins
ID: 9715099

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
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 

Author Comment

by:paulie99
ID: 9715237
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.
0
 
LVL 5

Expert Comment

by:brianadkins
ID: 9715327

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
0
 
LVL 16

Assisted Solution

by:_nn_
_nn_ earned 500 total points
ID: 9715403
Hi Paulie,

Are you sure of this ?

set FOLDER=C:\search_test\testUDebug2.txt

That should be a folder name, not a file name. If you need to parse specific files, you could do something like :

@echo off
:: global parameters, please adapt as needed
set FOLDER=C:\search_test
set MASK=*.*
:: or do you rather mean  ?
:: set MASK=testUDebug2.txt
set PATTERN=.[1-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 /C:"%PATTERN%" %1
if ERRORLEVEL 1 goto end
set /A FOUND=%FOUND%+1

:end
0
 

Author Comment

by:paulie99
ID: 9715438
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????

0
 
LVL 5

Expert Comment

by:brianadkins
ID: 9715458

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

-Brian
0
 
LVL 16

Expert Comment

by:_nn_
ID: 9715874
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

0
 

Author Comment

by:paulie99
ID: 9716049
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.
0
 
LVL 16

Accepted Solution

by:
_nn_ earned 500 total points
ID: 9716208
%$@#!!

That findstr thingie is slowly getting on my nerves... Paulie, would you mind using some real regexp tool like grep ?
Anyway, I tested that one, and it seems ok :

@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=\^<[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
0
 

Author Comment

by:paulie99
ID: 9716424
Hi _nn_

as always you come up trumps.

I will spilt the points in your favour with Brian.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question