1.

Solve : Searching entry within a file?

Answer»

Hi All,

I am trying to write a bat file that should check for a particular entry within a file. On finding, it should proceed to the next step or terminate right there.

I have managed to put in the FOLLOWING piece of code but it's not helping me with what I want to do:

Code: [Select]ECHO OFF
REM========================================

SET LOG=\tmp\check.log

REM ====================================================
REM 1) Check for entry "A"
REM ====================================================
E:
cd \ABC

IF EXIST temp.log.(FINDSTR "A" temp.log.)ELSE echo temp.log. missing >>%LOG%
IF %ERRORLEVEL% NEQ 0 (
ECHO Failed to find entry A in temp.log >>%LOG%
goto EXITERROR ) ELSE (
goto step2
)

REM ====================================================
REM 2) Check for entry "B"
REM ====================================================
:step2
E:
cd \ABC

IF EXIST temp.log.(FINDSTR "B" temp.log.)ELSE echo temp.log. missing >>%LOG%
IF %ERRORLEVEL% NEQ 0 (
ECHO Failed to find entry B in temp.log >>%LOG%
goto EXITERROR ) ELSE (
goto step3
)

REM ====================================================
REM 3) Check for entry "C"
REM ====================================================
:step3
E:
cd \ABC

IF EXIST temp.log.(FINDSTR "C" temp.log.)ELSE echo temp.log. missing >>%LOG%
IF %ERRORLEVEL% NEQ 0 (
ECHO Failed to find entry C in temp.log >>%LOG%
goto EXITERROR ) ELSE (
ECHO All entries found in the file>>%LOG%
)

REM ==========================
REM ERROR Code
REM ==========================
:EXITERROR
The file temp.log resides under the folder ABC under E: drive. I want to check for the entry "A" within this file before proceeding with the next step. The script should simply go to the next step if it finds, otherwise it should exit by alerting the user.

Please can you help?

Many thanksWas able to salvage "check for entry "A". The other letters should follow the same pattern.

Code: [Select]echo off
rem========================================

set log=\tmp\check.log

rem ====================================================
rem 1) check for entry "a"
rem ====================================================
e:
cd \abc

if exist temp.log (findstr "a" temp.log) else (echo temp.log. missing >>%log%)
if %errorlevel% neq 0 (
  echo failed to find entry a in temp.log >>%log%
  goto exiterror ) else ( goto step2
)

 c:\test>type  swados.bat
echo off
echo. > c:\tmp\check.log
set log=c:\tmp\check.log


cd c:\test\abc

if exist temp.log (findstr "a" temp.log) else (echo temp.log. missing >>%log%)
echo errorlevel=%errorlevel%
if %errorlevel% neq 0 (
  echo failed to find entry a in temp.log >>%log%
  goto exiterror ) else ( goto step2)

)
:exiterror
echo errorlevel=%errorlevel%
exit /b
:step2
cd c:\test\abc
findstr b temp.log

if EXIST temp.log.(FINDSTR "b" temp.log.)ELSE (echo temp.log. missing >>%log%)
echo errorlevel=%errorlevel%
if %errorlevel% NEQ 0 (
echo Failed to find entry B in temp.log >>%log%
goto exiterror ) else (
goto step3 )

:step3
cd c:\test\abc
findstr  c  temp.log

if EXIST temp.log.(findstr "c" temp.log.)else (echo temp.log. missing >>%log%)
if %errorlevel% NEQ 0 (
echo Failed to find entry C in temp.log >>%log%
goto exiterror ) else (
echo All entries found in the file>>%log%)

cd c:\tmp\
echo type  check.log
type check.log

Output:

c:\test> swados.bat
a
errorlevel=0
b
errorlevel=0
c
type  check.log

All entries found in the file

c:\tmp>

_______________________________________ ________


C:\test>cd  abc

C:\test\abc>dir
 C:\test\abc>type  temp.log
a
b
x


C:\test>type  sw2dos.bat
echo off
echo. > c:\tmp\check.log
set log=c:\tmp\check.log


cd c:\test\abc

if exist temp.log (findstr "a" temp.log) else (echo temp.log. missing >>%log%)
echo errorlevel=%errorlevel%
if %errorlevel% neq 0 (
  echo failed to find entry a in temp.log >>%log%
  goto exiterror ) else ( goto step2)

)
:exiterror
echo errorlevel=%errorlevel%
cd c:\tmp\
echo type  check.log
type check.log
pause
exit /b
:step2
cd c:\test\abc
findstr b temp.log

if EXIST temp.log.(FINDSTR "b" temp.log.)ELSE (echo temp.log. missing >>%log%)
echo errorlevel=%errorlevel%
if %errorlevel% NEQ 0 (
echo Failed to find entry B in temp.log >>%log%
goto exiterror ) else (
goto step3 )

:step3
cd c:\test\abc
findstr  c  temp.log

if EXIST temp.log.(findstr "c" temp.log.)else (echo temp.log. missing >>%log%)

if %errorlevel% NEQ 0 (
echo Failed to find entry C in temp.log >>%log%
goto exiterror ) else (
echo All entries found in the file>>%log%)

cd c:\tmp\
echo type  check.log
type check.log

Output:

C:\test>sw2dos.bat
a
errorlevel=0
b
errorlevel=0
errorlevel=1
type  check.log

Failed to find entry C in temp.log
Press any key to continue . . .

c:\tmp> Quote from: donald99 on December 17, 2010, 06:43:58 PM

c:\test>type  swados.bat

[etc]


Hi, Bill! No change in the posting style, I see.
Oh man, you spoiled it! Donald (aka Bill) has been BIRD dogging a few of my posts lately and offering up snippet explanations with his usual inimitable style. This certainly has added value to my usual battleship gray code boxes and has turned CH into a destination website.

Can't catch a break anywhere. Yes,  reply 2 added INSIGHT and output to the thread.
Now most readers can UNDERSTAND the question by Swados
and the solution offered by SidewinderHere we go again... 

Hello


Discussion

No Comment Found