Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi i am new to this, i want to search a string in a log file through batch file and if it found that string, it should print 'success' otherwise 'failure' message into a txt file.
Thanks in advance

@echo off
find "string" < my.log > nul
goto :%errorlevel%
:1
echo failure & goto :eof
:0
echo success
=====================================
If at first you don't succeed, you're about average.M2

thanks for the answer but it is not working , it giving success even though it is a failure.
i have written the below script but still it is not working
@echo off
find "Unix" <"C:\Cognos_Data\Logs\DynamicPackaging_PCConn.log"> nul
goto :%errorlevel%
:1
echo failure >"C:\Cognos_Data\Batch_Files\Incremental_batch_files\Test.txt"
:0
echo success >"C:\Cognos_Data\Batch_Files\Incremental_batch_files\Test.txt",Also i want how many times that word "unix" present in that log file ,
i.e.if that word "UNIX" in that log file present 5 times then success otherwise failure.
Thanks in advance

It goes to :0 because you left out:
goto :eof
=====================================
If at first you don't succeed, you're about average.M2

Thanks for the reply, but i tried the below code ,its still not working,
@echo off
find "Unix" <"C:\Cognos_Data\Logs\DynamicPackaging_PCConn.log"> nul
goto :%errorlevel%
:1
echo failure & goto :eof
>"C:\Cognos_Data\Batch_Files\Incremental_batch_files\Test.txt"
:0
echo success >"C:\Cognos_Data\Batch_Files\Incremental_batch_files\Test.txt"Please can you help me in this logic.
i need the batch file for this code
if that word "UNIX" in that log file present 5 times then success otherwise failure.Thank you very much

Hope this does the trick:
----------------------------------------------
@echo off & cls
type nul>tempsearch.txt::// WORD TO SEARCH FOR.
:search
find /i "unix" < "C:\Cognos_Data\Logs\DynamicPackaging_PCConn.log" >> tempsearch.txt::// CHECK IF WORD APPEARS 5+ TIMES.
set counter=0
for /f %%A in (tempsearch.txt) do (
set line=%%A
set /a counter +=1
)if %counter% GEQ 5 goto :GREATER_OR_EQUAL
goto :LESS:GREATER_OR_EQUAL
echo Your search word appears more than 5 times in your document.
echo success >> "C:\Cognos_Data\Batch_Files\Incremental_batch_files\Test.txt"
echo UNIX was present %counter% times >> "C:\Cognos_Data\Batch_Files\Incremental_batch_files\Test.txt"
pause>nul
goto :EOF:LESS
echo Your search word appears less than 5 times in your document.
echo success >> "C:\Cognos_Data\Batch_Files\Incremental_batch_files\Test.txt"
echo UNIX was present %counter% times >> "C:\Cognos_Data\Batch_Files\Incremental_batch_files\Test.txt"
pause>nul

Thanks a lot its workings, But still I have one more issue, i have function PCCONN in Cognos application ,
That Command should be execute first and then the remaining logic should run
But once that Function execution is done, bat file is get finish without executing the remaining code below it.
Is there any function i can give like -f after PCCON command so that it will execute the remaing code too after this command.
e.g.
REM --------- Step 1: Use pcconnect to Redirect the datasource ---------
cd C:\Program Files\cognos\c83\webapps\utilities\PCConn
pcconn -f C:\Cognos_Data\Batch_Files\DynamicPackaging\RedirectDS.txt > "C:\Cognos_Data\Logs\DynamicPackaging_PCConn.log"@echo off
type nul> tempsearch.txt::// WORD TO SEARCH FOR.
:search
find /i "UNIX" < "C:\Cognos_Data\Logs\DynamicPackaging_PCConn.log" >> tempsearch.txt::// CHECK IF WORD APPEARS 5+ TIMES.
set counter=0
for /f %%A in (tempsearch.txt) do (
set line=%%A
set /a counter +=1
)if %counter% GEQ 5 goto :GREATER_OR_EQUAL
goto :LESS:GREATER_OR_EQUAL
echo PCCON success >>"C:\Cognos_Data\Batch_Files\Cubemonitoring\Test.txt"
Success"
goto :EOF:LESS
echo PCCON Failure >> "C:\Cognos_Data\Batch_Files\Cubemonitoring\Test.txt"exit;

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |