Solved

A pattern to equal a wildcard

Posted on 2003-11-06
12
462 Views
Last Modified: 2010-05-18
Hi

I have the following script, but I need to be able to search for ERROR = 1 OR MORE, as the amont of errors in the file could be more than 1. 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
  • 3
  • 3
12 Comments
 
LVL 5

Expert Comment

by:SethHoyt
ID: 9693595
Do you want to add these values, or just count how many are 1 or more. Also, if it weren't 1 or more, would "ERROR =" still appear, and if so, what would appear after it?
0
 

Author Comment

by:paulie99
ID: 9693854
Hi

I just want to be able to search for ERROR = 1 or more, as the errors encountered may not allalways be one. So I just want to count how many are 1 or more. And what changes I will have to make to the above script?
0
 
LVL 5

Accepted Solution

by:
SethHoyt earned 100 total points
ID: 9693872
Ok, but what else could come after "ERROR = " if not 1 or more? In other words, could you have "ERROR = 0" or a negative number or something else? What are all the possibilities?
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 5

Expert Comment

by:SethHoyt
ID: 9693881
If the only thing that can come after "ERROR = " is 1 or more, then just delete the 1 from the pattern by changing

set PATTERN=ERROR = 1

to

set PATTERN=ERROR =


-Seth
0
 
LVL 5

Expert Comment

by:SethHoyt
ID: 9693899
If the only other thing that could come after "ERROR = " is a zero, then you could add a line to the bottom right before :end, such as this:

for /f "tokens=1-2 delims=:" %%a in ('find /C "ERROR = 0" %1') do set /A COUNT=%COUNT%-%%b
0
 

Author Comment

by:paulie99
ID: 9694043
In answer to your questions "ERROR = " CAN BE ANYTHING FROM 1 UPWARDS, that is 2 3 4 5 6 7 8.....1000...

So therefore Error = 0 is the only anything it could possibly be apart from the above. So if it is Error = 0 then netsend GOOD. All other results to Error = * will be netsend bad.
0
 
LVL 5

Expert Comment

by:SethHoyt
ID: 9694342
So "ERROR = 0" is possible then?

Your remarks seem to contradict, but I think in the first you meant those are the acceptable values.

In that case, try adding the line I gave to subtract the zero cases from the count.
0
 
LVL 5

Expert Comment

by:SethHoyt
ID: 9694364
It would seem to me that there may be a simpler way to do what you are trying to accomplish, but hopefully this will do it for you.
0
 
LVL 8

Expert Comment

by:K_2K
ID: 9694878
This changes the intent of the code,  does it change it too much?  
For a log file with two reports:
     ERROR = 2
     ERROR = 5
The old code would report 2 error lines found.  Seth has you covered on that by subtracting the lines that are not errors.  I find it easier to report 7 total errors in 1 bad file, and hopefully more helpfull:


My 2¢,
2K
(\o/)


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

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

echo Found %COUNTerr% total errors in %COUNTf% bad files. Sending message...
if %COUNTerr% GEQ 1 (
  net send pc-000024 test-BAD - %COUNTerr% total errors in %COUNTf% bad files.
) else (
  net send pc-000024 test-GOOD - %COUNTerr% total errors in %COUNTf% bad files.
)
echo Message sent.
goto end


:increment
:: Pull error line and parse it
set zgood=yes
for /f "tokens=3" %%a in ('findstr /b "%PATTERN%" %1') do (
    set /A COUNTerr=%COUNTerr%+%%a
    if  NOT %%a.==. if %%a GTR 0 set zgood=no
)
if %zgood%.==no. set /A COUNTf=%COUNTf% + 1


:end
0
 
LVL 8

Assisted Solution

by:K_2K
K_2K earned 400 total points
ID: 9694894
(By "this changes" I mean "my code changes" - not referring to the previous questions that clarified and were not changing it but helping fix it.)
0
 

Author Comment

by:paulie99
ID: 9695030
I ran the above script, which ignores any value in a file called *1.* which is what I want. I get a test-good message when I run the script against text files which contain ERROR = 0. BUT WHEN I CHANGE THE VALUE FROM to 1 or 2 or 3 then I continue to get a test-good message, which is not correct. To be honest I have no idea of what the increment: section does????

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

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

echo Found %COUNTerr% total errors in %COUNTf% bad files. Sending message...
if %COUNTerr% GEQ 1 (
  net send pc-000024 test-BAD - %COUNTerr% total errors in %COUNTf% bad files.
) else (
  net send pc-000024 test-GOOD - %COUNTerr% total errors in %COUNTf% bad files.
)
echo Message sent.
goto end


:increment
:: Pull error line and parse it
set zgood=yes
for /f "tokens=3" %%a in ('findstr /b "%PATTERN%" %1') do (
    set /A COUNTerr=%COUNTerr%+%%a
    if  NOT %%a.==. if %%a GTR 0 set zgood=no
)
if %zgood%.==no. set /A COUNTf=%COUNTf% + 1


:end
0
 
LVL 8

Expert Comment

by:K_2K
ID: 9701341
:increment
:: Pull error line and parse it
set zgood=yes
for /f "tokens=3" %%a in ('findstr /b "%PATTERN%" %1') do (
   set /A COUNTerr=%COUNTerr%+%%a
   if  NOT %%a.==. if %%a GTR 0 set zgood=no
)
if %zgood%.==no. set /A COUNTf=%COUNTf% + 1


That should open the file who's name is in the call line, add the total error count from all error lines in it to COUNTerr, and add one to COUNTf only if there is one or more error line in the file that is 1 or higher.

Pseudo-code:
assume file is good (no errors)
loop through lines with pattern at the beginning of line, using the third word on each matching line as %a
     add %a to the error count (adding 0 shouldn't matter)
     change to no good if number is over 0  ( doing this for multiple error lines should't matter)
end loop
if this file no good add one to file count COUNT


Troubleshooting: numbers does the line report?
echo Found %COUNTerr% total errors in %COUNTf% bad files. Sending message...


Change to this and send a partial list including portion related to at least one file with error in it.  This should help us determing if words and tokens are correctly separated.  
We may also determing that it's better to use "=" as a delimiter, so the number would become token 2 regardless of spaces or tabs.

:increment
:: Pull error line and parse it
echo Checking errors in: %1
set zgood=yes
for /f "tokens=1-3" %%a in ('findstr /b "%PATTERN%" %1') do (
   echo a: %%a: b: %%b: c: %%c:
   set /A COUNTerr=%COUNTerr%+%%c
   if  NOT %%c.==. if %%c GTR 0 set zgood=no
)
if %zgood%.==no. set /A COUNTf=%COUNTf% + 1


Good Luck,
2K
(\o/)
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

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…
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…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
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 …

830 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