Solved

Search on a wildcard

Posted on 2003-11-07
12
1,077 Views
Last Modified: 2008-03-17
Hi

I have a script which I wish to search a directory that contains a number of log files which are created by a build script which will contain values such as:
ERROR = 0
another log could contain
ERROR = 1
and another log file could contain
ERROR = 3
I have asked the quested before but with no result. I have the following script which was created by expert _nn_

In this script I search for a patern equal to ERROR = 1 which works fine, but I need to able to cover other values that "ERROR =" can contain for example 2 or 3 or 4 or 5.....or 100 etc, depending on the number of errors that my build script emits. If "ERROR = 0" then as the script covers this is to be ignored. All suggestions welcome:

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

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

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

:increment
:: filenames without path won't have a ':'
for /f "tokens=1-2 delims=:" %%a in ('find /C "%PATTERN%" %1') do set /A COUNT=%COUNT%+%%b

:end

 
 
0
Comment
Question by:paulie99
  • 6
  • 6
12 Comments
 
LVL 16

Expert Comment

by:_nn_
Comment Utility
Hi Paulie,

I thought the solutions given by the other experts would work. Maybe you could ask them for "tuning" their proposals.

In case of, I would propose a small change based on regular expressions support in findstr :

@echo off
:: global parameters, please adapt as needed
set FOLDER=C:\test_search
:: rationale : the first digit must not be 0
set PATTERN=ERROR.=.[1-9]

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

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

:increment
:: filenames without path won't have a ':'
for /f "delims=" %%a in ('findstr /R "%PATTERN%" %1 ^| find /C /V ""') do set /A COUNT=%COUNT%+%%a

:end
0
 

Author Comment

by:paulie99
Comment Utility
Hi
I used your above script against 2 files one called test1.txt which was ignored, thanks for that code.

But when it reads the other file which has ERROR = 0 it comes back test-bad, when it should be test-good. When I change the contents to ERROR = 1
OR ERROR = 4
I get the same message back test-bad.

Any suggestions are welcome.
Paul
0
 

Author Comment

by:paulie99
Comment Utility
Hi
I used your above script against 2 files one called test1.txt which was ignored, thanks for that code.

But when it reads the other file which has ERROR = 0 it comes back test-bad, when it should be test-good. When I change the contents to ERROR = 1
OR ERROR = 4
I get the same message back test-bad.

All suggestions are welcome.
Paul
0
 
LVL 16

Expert Comment

by:_nn_
Comment Utility
Strange... Can you please tell me what is the output of this command on your system :

findstr /R "ERROR.=.[1-9]" filename | find /C /V ""

(replace filename with the actual file name of a file which has and "ERROR = 0" inside)
0
 
LVL 16

Expert Comment

by:_nn_
Comment Utility
sorry, which has a "ERROR = 0" inside.
0
 

Author Comment

by:paulie99
Comment Utility
Hi

Below is the output after running:
findstr /R "ERROR.=.[1-9]" filename | find /C /V ""
and the contents of the c:\test_search
C:\test_search>dir
 Volume in drive C has no label.
 Volume Serial Number is 583F-FC62

 Directory of C:\test_search

11/07/03  02:35p        <DIR>          .
11/07/03  02:35p        <DIR>          ..
11/07/03  01:51p                     9 test1.txt
11/07/03  01:51p                     9 testA.txt
               4 File(s)             18 bytes
                          3,030,921,216 bytes free

C:\test_search>findstr /R "ERROR.=.[1-9]" testa.txt | find /C /V ""
1

C:\test_search>

Test1.txt contains   ERROR = 1
TESTA.txt contains ERROR = 0
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 16

Expert Comment

by:_nn_
Comment Utility
Just to make sure, what gives

findstr /R "ERROR.=.[1-9]" testa.txt

alone ?
0
 

Author Comment

by:paulie99
Comment Utility
C:\test_search>findstr /R "ERROR.=.[1-9]" testa.txt

C:\test_search>

testa.txt contans:
ERROR = 0
0
 
LVL 16

Expert Comment

by:_nn_
Comment Utility
Ok. well it seems that this findstr returns at least an emtpy line (it does not on my 2K). So, I guess that just tuning the line like this

for /f "delims=" %%a in ('findstr /R "%PATTERN%" %1 ^| find /C /V ""') do set /A COUNT=%COUNT%+%%a-1

could work.
0
 

Author Comment

by:paulie99
Comment Utility
Alas no,

no matter what value I put into the texta.txt file be it
ERROR = 0
ERROR = 1
ERROR = 3

I get the test-good message which is fine for ERROR = 0 but not for the other values listed above.
0
 
LVL 16

Accepted Solution

by:
_nn_ earned 500 total points
Comment Utility
Grmbl... it works here...

A question : do you care how many occurences there are ? If not, please try :

@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
 

Author Comment

by:paulie99
Comment Utility
No

If there is an error in the logs then iwant to know.
Thanks for all your patience, have a good weekend
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

772 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

10 Experts available now in Live!

Get 1:1 Help Now