GURLOVE CHOPRA
GURLOVE CHOPRA

Reputation: 85

How to print last line of a .log file using batch script

I am trying to read and print last lines of files with extensions like .txt,.logs etc. using batch script. So as per now I have tried following code on .txt file which is working fine:

for /f "delims==" %%a in (D:\error.txt) do set lastline=%%a
echo %lastline%

But similarly when I am trying to do this on .log file , I am unable to achieve the desired output, further it gives the error :

system cannot find the file

Code which I am trying is as follows:

for /f "delims==" %%a in (C:\Program Files (x86)\Apache Software Foundation\Apache2.2\logs\error.log) do set lastline=%%a
echo %lastline%

Please help me guys, and please point me where I am going wrong

Upvotes: 3

Views: 13666

Answers (1)

Compo
Compo

Reputation: 38589

You need to use double-quotes because of spaces in your string. As a result you'll need the UseBackQ option in your For /F.

For /F "UseBackQ Delims==" %%A In ("D:\error.txt") Do Set "lastline=%%A"
Echo %lastline%

and:

For /F "UseBackQ Delims==" %%A In ("%ProgramFiles(x86)%\Apache Software Foundation\Apache2.2\logs\error.log") Do Set "lastline=%%A"
Echo %lastline%

Upvotes: 7

Related Questions