Monday, March 19, 2012

DOS Batch file To search string in given file

 

Requirement:

To Search the given files under current directory for given strings. If anything found to be written to a file.

Sample Screen:

image

 

Main Script: CHK_Error.bat

@echo off

set fname=
set filename=
set yyyymmdd=
set hhmm=

IF %1. NEQ . (set fname="C:\Accounts\CalWIN\Build\SQLServer\DOS_Batch\%1*.lst") ELSE ( GOTO _Usage )

for /F "tokens=2-4 delims=/ " %%i in ('date /t') do set yyyymmdd=%%k%%i%%j

for /F "tokens=1-2 delims=: " %%l in ('time /t') do set hhmm=%%l%%m

set filename=Error_%yyyymmdd%%hhmm%.txt

echo "Error Log file name is : " %filename%

echo "files to be searched : " %fname%

findstr /i /n /g:search.txt  %fname% > %filename%

if %errorlevel% == 0 (GOTO :_Error_Found ) else  (
Echo  **  No Errors found    **
Goto :END )

:_Error_Found
notepad %filename%
Goto :END

:_Usage
echo " Usage is chkerrors.bat <filestring>

:END
@echo on

Search.txt

msg
error
severity
warning
fail
fatal

Input Files: file1.lst

dgadagdagad
abacdefghijklmnopqrstuvwxyzagadgda
gagad

st messgae
g

adgadg

agadgadgadadadsflmsvbkobsagad
dgadg
ad
ddad

dgadgadg
error 121

dgad
gdagad

gadagad

gagad

msg 128, 9341353513

Input Files: file2.lst

test messgae
g
sagad
dgadg
ad
ddad

dgadgadg
error 121

dgad
gdagad

gadagad

gagad

msg 128, 9341353513

No comments: