[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

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

Search on a wildcard

Hi

I have a script which I wish to search a directory that contains a number of log files which are created by a build script which will contain values such as:
ERROR = 0
another log could contain
ERROR = 1
and another log file could contain
ERROR = 3
I have asked the quested before but with no result. I have the following script which was created by expert _nn_

In this script I search for a patern equal to ERROR = 1 which works fine, but I need to able to cover other values that "ERROR =" can contain for example 2 or 3 or 4 or 5.....or 100 etc, depending on the number of errors that my build script emits. If "ERROR = 0" then as the script covers this is to be ignored. 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
  • 6
1 Solution
 
_nn_Commented:
Hi Paulie,

I thought the solutions given by the other experts would work. Maybe you could ask them for "tuning" their proposals.

In case of, I would propose a small change based on regular expressions support in findstr :

@echo off
:: global parameters, please adapt as needed
set FOLDER=C:\test_search
:: rationale : the first digit must not be 0
set PATTERN=ERROR.=.[1-9]

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 "delims=" %%a in ('findstr /R "%PATTERN%" %1 ^| find /C /V ""') do set /A COUNT=%COUNT%+%%a

:end
0
 
paulie99Author Commented:
Hi
I used your above script against 2 files one called test1.txt which was ignored, thanks for that code.

But when it reads the other file which has ERROR = 0 it comes back test-bad, when it should be test-good. When I change the contents to ERROR = 1
OR ERROR = 4
I get the same message back test-bad.

Any suggestions are welcome.
Paul
0
 
paulie99Author Commented:
Hi
I used your above script against 2 files one called test1.txt which was ignored, thanks for that code.

But when it reads the other file which has ERROR = 0 it comes back test-bad, when it should be test-good. When I change the contents to ERROR = 1
OR ERROR = 4
I get the same message back test-bad.

All suggestions are welcome.
Paul
0
Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

 
_nn_Commented:
Strange... Can you please tell me what is the output of this command on your system :

findstr /R "ERROR.=.[1-9]" filename | find /C /V ""

(replace filename with the actual file name of a file which has and "ERROR = 0" inside)
0
 
_nn_Commented:
sorry, which has a "ERROR = 0" inside.
0
 
paulie99Author Commented:
Hi

Below is the output after running:
findstr /R "ERROR.=.[1-9]" filename | find /C /V ""
and the contents of the c:\test_search
C:\test_search>dir
 Volume in drive C has no label.
 Volume Serial Number is 583F-FC62

 Directory of C:\test_search

11/07/03  02:35p        <DIR>          .
11/07/03  02:35p        <DIR>          ..
11/07/03  01:51p                     9 test1.txt
11/07/03  01:51p                     9 testA.txt
               4 File(s)             18 bytes
                          3,030,921,216 bytes free

C:\test_search>findstr /R "ERROR.=.[1-9]" testa.txt | find /C /V ""
1

C:\test_search>

Test1.txt contains   ERROR = 1
TESTA.txt contains ERROR = 0
0
 
_nn_Commented:
Just to make sure, what gives

findstr /R "ERROR.=.[1-9]" testa.txt

alone ?
0
 
paulie99Author Commented:
C:\test_search>findstr /R "ERROR.=.[1-9]" testa.txt

C:\test_search>

testa.txt contans:
ERROR = 0
0
 
_nn_Commented:
Ok. well it seems that this findstr returns at least an emtpy line (it does not on my 2K). So, I guess that just tuning the line like this

for /f "delims=" %%a in ('findstr /R "%PATTERN%" %1 ^| find /C /V ""') do set /A COUNT=%COUNT%+%%a-1

could work.
0
 
paulie99Author Commented:
Alas no,

no matter what value I put into the texta.txt file be it
ERROR = 0
ERROR = 1
ERROR = 3

I get the test-good message which is fine for ERROR = 0 but not for the other values listed above.
0
 
_nn_Commented:
Grmbl... it works here...

A question : do you care how many occurences there are ? If not, please try :

@echo off
:: global parameters, please adapt as needed
set FOLDER=C:\test_search
set PATTERN=ERROR.=.[1-9]

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

echo Found occurences in %FOUND% files. Sending message...
if %FOUND% GEQ 1 (
  net send pc-000024 test-BAD
) else (
  net send pc-000024 test-GOOD
)
echo Message sent.
goto end

:increment
findstr /R "%PATTERN%" %1
if ERRORLEVEL 1 goto end
set /A FOUND=%FOUND%+1

:end
0
 
paulie99Author Commented:
No

If there is an error in the logs then iwant to know.
Thanks for all your patience, have a good weekend
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

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