2017-02-23 72 views
-1

我有一個將日誌寫入acc.txt文件的進程,我試圖在.txt文件中找到某個字符串時重新啓動進程。一旦找到字符串並與我正在查找的字符匹配,就應該清除acc.txt內容並且必須終止進程。如果在文本文件中找到字符串,批處理腳本將終止進程

更新:修正了錯誤,請參閱下面的解決方案。

+2

這不是一個問題,而是一個任務請求。請提供您嘗試過的代碼並描述您遇到的問題。請參閱本幫助主題:[問]! – aschipfl

回答

0

您可以使用findstrtaskkill命令,例如:

@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 
+0

似乎我只收到一個'string not found'消息,雖然Error8902文本確實存在於acc.txt文件中。 – Chris

+0

我替換爲:找到「Error8902」acc.txt> nul 2>&1 && goto:stringFound並且現在似乎工作,問題在於acc.txt內容完好無損並且不會被刪除。同時似乎以管理員身份運行批處理腳本返回字符串未找到 – Chris

+0

@Chris,因爲以管理員身份運行批處理文件將工作目錄設置爲System32目錄。 – Squashman

相關問題