?
Solved

A pattern to equal a wildcard

Posted on 2003-11-06
12
Medium Priority
?
465 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
[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
  • 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 200 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
Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

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…
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 brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

762 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