-1

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.

Chris
  • 41
  • 2
  • 10
  • 2
    This is not a question but a task request. Please provide the code you have tried and describe what you have problems with. Consult this help topic: [ask]! – aschipfl Feb 23 '17 at 18:39

1 Answers1

0

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
Sam Denty
  • 3,693
  • 3
  • 30
  • 43
  • Seems I'm getting only a `string not found` message, although the Error8902 text does exists inside of acc.txt file. – Chris Feb 23 '17 at 18:47
  • I replaced with: find "Error8902" acc.txt >nul 2>&1 && goto :stringFound and seems to work now, the problem is that the acc.txt contents are intact and don't get deleted. At the same time seems that running the batch script as administrator returns string not found – Chris Feb 23 '17 at 19:04
  • @Chris, Because running a batch file as administrator sets the working directory to the System32 directory. – Squashman Feb 23 '17 at 19:18
  • You need to remove the /F option. The /F option is for a file list. He just needs to parse the file acc.txt. – Squashman Feb 23 '17 at 19:19
  • Oh, right. Totally forgot about this part, thank you. So using line `find "Error8902" acc.txt >nul 2>&1 && goto :stringFound` will make the script as intended, the issue is that `acc.txt` contents are intact and don't get deleted. Any ideas? – Chris Feb 23 '17 at 19:21
  • @Chris the TASKKILL command will kill the process as long as you are using it correclty. If you need to wipe out the file then put in a delete command. – Squashman Feb 23 '17 at 19:25
  • @Chris see updated version of the answer, now supports being launched as administrator – Sam Denty Feb 23 '17 at 19:37
  • I managed to modify the script for my needs with a few extra commands. Thanks for all your help, @SamuelDenty – Chris Feb 24 '17 at 19:22
  • @Chris Glad I could help – Sam Denty Feb 24 '17 at 20:03