Solved

Search on a wildcard

Posted on 2003-11-07
12
1,079 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_
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
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
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 500 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

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Using dates in 'DOS' batch files has always been tricky as it has no built in ways of extracting date information.  There are many tricks using string manipulation to pull out parts of the %date% variable or output of the date /t command but these r…
YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

773 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