?
Solved

Search on a wildcard

Posted on 2003-11-07
12
Medium Priority
?
1,083 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 6
12 Comments
 
LVL 16

Expert Comment

by:_nn_
ID: 9700926
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
ID: 9701208
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
ID: 9701209
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
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
LVL 16

Expert Comment

by:_nn_
ID: 9701306
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_
ID: 9701320
sorry, which has a "ERROR = 0" inside.
0
 

Author Comment

by:paulie99
ID: 9701410
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
 
LVL 16

Expert Comment

by:_nn_
ID: 9702131
Just to make sure, what gives

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

alone ?
0
 

Author Comment

by:paulie99
ID: 9702216
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_
ID: 9702258
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
ID: 9702503
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 2000 total points
ID: 9702577
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
ID: 9702686
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

One of my most closely kept secrets is revealed in this discussion How to output text on the same line This question was recently posted in EE by Simon336697 (http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_2459…
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…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Suggested Courses

752 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