Search code examples
batch-filereplacekill-processstring-searchtext-search

Batch script to kill process if string is found in text file


I have a process that writes logs to a acc.txt file and I'm trying to restart the process when a certain string has been found inside of that .txt file. Once the string is found and matches with what I'm looking for, the acc.txt contents should be cleared and process must be killed.

UPDATE: Errors fixed, see solution below.


Solution

  • You can use the findstr and taskkill commands, for example:

    @echo off
    pushd %~dp0
    
    :loop
       find "Error8902" acc.txt >nul 2>&1 && goto :stringFound
       echo String not found
       timeout /nobreak 1 >nul      & :: Interval between scanning
    goto :loop
    
    
    :stringFound
       echo String found, killing process
       taskkill /f /t /im process.exe
       del /F acc.txt
       pause