How to write a batch script to loop through logfiles in directory and generate a “filename.found” if i find the string “found” in the log file?

别来无恙 提交于 2020-06-09 05:31:25

问题


I have a directory "D:\logs" consisting of many log files eg: HRS.log, SRM.log, KRT.log, PSM.log etc. Each of this log file may or may not have a string "found" inside them. If the log file contains the string "found", then i have to generate "fileName.found" eg: "SRM.found" file in "D:\flags"folder. i have written the following script but not able to proceed further:

@echo off
setlocal ENABLEDELAYEDEXPANSION

for  %%f IN ("D:\logs\*.log") do (
    findstr /i "found" "%%f" >NUL
    if  "!ERRORLEVEL!"=="0" (
    echo.>"D:\flags\%%f.found"
    ) 
    )
    pause 
    exit /b
)

回答1:


@echo off

for /f "delims=" %%A in (
    '2^>nul findstr /i /m "found" D:\logs\*.log'
) do echo( > "D:\flags\%%~nA.found"

findstr /i can search in the files for the case insensitive string found and use of argument /m which allows for return of only the filepaths that contain that string. This can make it more efficient as the for /f command returns the filepaths only of interest.

%%~nA uses a for variable modifier of n which is the filename with no extension. View for /? for more information about the modifiers available.




回答2:


ok, so here's the solution i found for the above question that i asked:

@echo off
setlocal enabledelayedexpansion

for  %%f IN ("D:\logs\*.log") do (
    find "found" "%%f" >NUL
    if  "!ERRORLEVEL!"=="0" (
        echo.>"D:\flags\%%~nf.found"
    ) 
)
pause
exit /b
)


来源:https://stackoverflow.com/questions/61516110/how-to-write-a-batch-script-to-loop-through-logfiles-in-directory-and-generate-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!