Search code examples
powershellbatch-filecmdfindstr

Looking for a string on a constantly changing file with a batch script


I have a .log file that is constantly being added new lines and I want to have a batch script that detects when the new line has a specific string.

First, I copy the .log file and change it into a .txt Then I read the last line

Here is an example of what a line in the .txt that should be detected is:

[05:35:26] [Client thread/INFO]: [CHAT] §6§lP§e§lrof

Here is the code I'm using:

    :search
    del C:\Users\Diogo\Desktop\Detector\latest.txt
    del C:\Users\Diogo\Desktop\Detector\latest.log
    xcopy /s "C:\LocationofFile\latest.log" "C:\Users\Diogo\Desktop\Detector"
    RENAME "C:\Users\Diogo\Desktop\Detector\latest.log" "latest.txt"
    @echo off & setlocal enabledelayedexpansion
    for /f "tokens=*" %%c in (C:\Users\Diogo\Desktop\Detector\latest.txt) do (
    set temp=%%c
    )
    echo !temp!
    (echo !temp! | findstr /i /c:"§6§lP§e§lrof" >nul) && (GOTO found) || (echo Variable does not have the string "§6§lP§e§lrof")
    Endlocal
    GOTO search

    :found
    pause

For some reason it doesn't detect it.
Any ideas?

Thank you in advance :D

EDIT: tried Powershell, here is the code right now (yes, I am searching the line for 2 different things):

  while ($True) {
Write-Output 'Enter <ctrl><c> to break out of this loop.'
Start-Sleep -Seconds 1
Copy-Item -LiteralPath "C:\LocationOfFile\latest.log" -Destination "C:\Users\Diogo\Desktop\Detector"
Rename-Item -Path "C:\Users\Diogo\Desktop\Detector\latest.log" -NewName "latest.txt"
Get-Content -Path "latest.txt" -tail 1 -wait | Select-String -Quiet '§6§lP§e§lrof'
if (System.Boolean -eq True) {
Invoke-Item result1.bat
pause
}
else {
Get-Content -Path "latest.txt" -tail 1 -wait | Select-String -Quiet 'spawned'
if (System.Boolean -eq True) {
Invoke-Item result2.bat
pause
}
else {
Nope
pause
}
}
}

Solution

  • The problem with your batch is the '§' characters in the search string. FindStr only works properly when the search string contains only ASCII characters, which '§' is not.

    Quote from https://stackoverflow.com/a/8844873/1278704

    Searching Unicode files

    FINDSTR cannot properly search most Unicode (UTF-16, UTF-16LE, UTF-16BE, UTF-32) because it cannot search for nul bytes and Unicode typically contains many nul bytes.

    However, the TYPE command converts UTF-16LE with BOM to a single byte character set, so a command like the following will work with UTF-16LE with BOM.

    type unicode.txt|findstr "search" Note that Unicode code points that are not supported by your active code page will be converted to ? characters.

    It is possible to search UTF-8 as long as your search string contains only ASCII. However, the console output of any multi-byte UTF-8 characters will not be correct. But if you redirect the output to a file, then the result will be correctly encoded UTF-8. Note that if the UTF-8 file contains a BOM, then the BOM will be considered as part of the first line, which could throw off a search that matches the beginning of a line.

    It is possible to search multi-byte UTF-8 characters if you put your search string in a UTF-8 encoded search file (without BOM), and use the /G option.

    So basically you have two options:

    • A: Use the Workaround described in the Quote above
    • B: Use another, more powerful search function like Powershell's select-string