【问题标题】:Using CMD or POWERSHELL to find failed log files使用 CMD 或 POWERSHELL 查找失败的日志文件
【发布时间】:2019-11-28 00:52:58
【问题描述】:

我有数百个日志文件import.log,一个成功的日志文件有最后一行“Task finished successfully”。

每个日志文件都在一个单独的目录中,所以我的搜索任务是遍历目录。读取每个import.log,并报告失败import.log的完整路径。

失败的import.log 确实包含“Task finished successfully”。 或者,import.log 的最后一行不是“Task finished successfully

我目前的解决方案我只找到成功的import.log

findstr /s /m "Task finished successfully" import.log

请告知如何查找失败的日志文件首选 CMD 工具,POWERSHELL 解决方案是第二选择。

【问题讨论】:

  • 找不到字符串时,该命令设置错误。您可以使用|| 指定失败时的处理方式。
  • 感谢 avery_larry,我创建了一个笨重的解决方案并在下面发布。
  • findstr "Task finished successfully" finds every line that contains at least one of the three words. Use the /c` 开关:findstr /mc:"Task finished successfully" 仅查找完整的字符串。

标签: powershell cmd findstr


【解决方案1】:

我知道这是您的第二选择,但这是您正在寻找的强大外壳。

$folder = "C:\folder\test\*"
$files = gci -Path $folder -Force -recurse -File -include "*.log"

foreach ($file in $files){
    $content = gc $file.FullName -raw | Out-Null
    if($content -match "Task finished successfully"){
        Write-host $File.Name imported successfully
        # Do more stuff. 
    }
    elseif((Get-Item $file.FullName).length -eq 0){
        Write-host $file is empty.
        #Do more stuff
    }
    else{
        Write-host $file import was unsuccessful.
        #Do more stuff
    }

}

您可以将 powershell 脚本命名为“checklogs.ps1”,然后使用以下命令从 .bat 中调用它:

@echo off
powershell.exe -executionpolicy bypass -file "C:\folder\checklogs.ps1"

【讨论】:

  • 添加了 -recurse
  • 我试图理解。不过感谢您的建议。
【解决方案2】:

关注avery_larry 的有用评论。我创建了一个可行的笨重解决方案。

我写了一个批处理文件find-failed-imports.bat

find-failed-imports.bat

echo off

for /r ..  %%a in (import.l?g) do (findstr "Task finished successfully" "%%a" >nul  || echo %%a failed)

欢迎任何更好的解决方案。

【讨论】:

  • 我对这个问题的评论也适用于这里。或者,您可以在此处使用find "Task finished successfully"(因为您不需要从findstr 切换/m
【解决方案3】:

尝试使用某个任务不成功的日志文件。 获取文件中的结果时(也在 Crtl + c 中)。

我建议这样:


@echo off 

set "_err=.\log_error.log" && set "_str=Task.finished.successfully" 

for /r . %%a in (import.log)do findstr "%_str%" "%%~a" >nul || echo=%%~a>>"%_err%"
if exist "%_err%" type "%_err%"|clip & type "%_err%"
exit /b 

观察:

  • Findstr 将查找/检查此字符串“任务已成功完成”

  • 并且可以类似地找到这个:“任务完成unsuccessfully”


为了成功阻止un,请使用:"Task.finished.successfully"

使用 [.] == wildcard: any character

阅读更多关于 FindSTR in this link


  • 更新:建议检查 name == imp*error.log 和 size > 0 的文件的大小

@echo off && cd /d "%~dp0"

set "err=.\log_error.log" && set "str=Task finished successfully"

for /f tokens^=* %%a in ('dir /s /b imp*.log')do if %%~Za neq 0 (
 echo %%~nxa|find /i ".error.log" >nul && echo="%%~fa" >>"%err%"
 ) || ( find /i /v "%str%" "%%~a" >nul && echo="%%~fa" >>"%err%")

(if exist "%err%" type "%err%" | clip && type "%err%") && exit /b 

对不起我的英语


【讨论】:

  • 感谢您的贡献。我遵循您的策略:我将import.error.log 作为处理阶段的一部分。并在分析阶段检查import.error.log size > 0。
  • 而不是 REGEX,最好使用/c 开关或find 命令。 (不太可能发生,但您的 REGEX 也会匹配 `Taskyfinishedxsuccesfully")
  • 在这种情况下,我将 in (im*.log) 替换为 in (import.log)
  • @Stepahn 谢谢你的评论,我同意你的看法,但是,我不可能在这个字符串中出现这个字符串 'Taskyfinishedxsuccesfully'输出可预测的任务。
  • @DudiBoy Ops,您需要检查 import.error.log 的大小吗?我不确定我是否理解您的最后评论。
猜你喜欢
  • 2021-10-12
  • 1970-01-01
  • 1970-01-01
  • 2019-09-16
  • 2022-01-10
  • 1970-01-01
  • 2016-06-21
  • 1970-01-01
  • 2016-10-02
相关资源
最近更新 更多