Link to home
Start Free TrialLog in
Avatar of paulie99
paulie99

asked on

A pattern to equal a wildcard

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

 
Avatar of SethHoyt
SethHoyt

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?
Avatar of paulie99

ASKER

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?
ASKER CERTIFIED SOLUTION
Avatar of SethHoyt
SethHoyt

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
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
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.
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.
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.
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
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
: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/)