Solved

A pattern to equal a wildcard

Posted on 2003-11-06
12
458 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
 
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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…
TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

706 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

12 Experts available now in Live!

Get 1:1 Help Now