Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 467
  • Last Modified:

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

 
0
paulie99
Asked:
paulie99
  • 6
  • 3
  • 3
2 Solutions
 
SethHoytCommented:
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
 
paulie99Author Commented:
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
 
SethHoytCommented:
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: 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.

 
SethHoytCommented:
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
 
SethHoytCommented:
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
 
paulie99Author Commented:
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
 
SethHoytCommented:
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
 
SethHoytCommented:
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
 
K_2KCommented:
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
 
K_2KCommented:
(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
 
paulie99Author Commented:
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
 
K_2KCommented:
: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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

  • 6
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now