Search code examples
windowsbatch-filefor-loopdos

Getting file path within dos batch file within for loop


I'm trying to create a batch file that will go into a directory and look for log files and then search the log files to see if the word "ERROR:" is in the start of a line. That part I have figured out. My issue now is trying to create a display row to show what directory i'm in with *.log after it.

For example

-------c:\temp*.log-----------------

--c:\temp\temp1.log

--c:\temp\temp2.log

ERROR: Temp error

-------c:\temp\subdir*.log-----------------

--c:\temp\subdir\temp3.log

ERROR: This is an error

--c:\temp\subdir\temp4.log

This is the code i'm currently using

Set _OutFile=c:\temp\check_error.txt
If Exist "%_OutFile%" Del "%_OutFile%"
For /F "Tokens=* Delims=" %%I In ('Dir /b "c:\temp\*.log"') Do (
(Echo.
 Echo.---------- %%~I
 )>>"%_OutFile%"
Findstr /B /C:"ERROR:"  "%%I">>"%_OutFile%"
)

This will only produce

---------- TempData.log

It doesn't show the main path or the files path.


Solution

  • Change this line:

    Echo.---------- %%~I
    

    to this:

    Echo.---------- %%~fI