?
Solved

Search on a wildcard

Posted on 2003-11-07
12
Medium Priority
?
1,084 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
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 
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

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

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…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

649 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