Solved

search wildcard

Posted on 2003-11-10
12
1,595 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
Comment Utility

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

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
 

Author Comment

by:paulie99
Comment Utility
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
Comment Utility

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
Comment Utility
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:paulie99
Comment Utility
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
Comment Utility

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

-Brian
0
 
LVL 16

Expert Comment

by:_nn_
Comment Utility
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
Comment Utility
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
Comment Utility
%$@#!!

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
Comment Utility
Hi _nn_

as always you come up trumps.

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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

771 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now