title: Batchfile: read last lines from logfiles and copy them to a new file
body: <p>This is my first posting so if the format is not as it supposed to be please excuse me for this. (Suggestions for </p>
  
  <p>improvement are welcome.)</p>
  
  <p>I am trying to create a batchfile that will read last lines from logfiles and copy them to a new file. </p>
  
  <p>Until now I have found <a href="https://stackoverflow.com/questions/1632300/bat-file-to-read-and-copy-bottom-16-lines-from-a-text-file-to-another-one"><strong>here</strong></a> a way to read the last line. </p>
  
  <p>Code would be something like:</p>
  
  <pre><code>for /f %%i in ('find /v /c "" ^&lt; someFile.txt') do set /a lines=%%i
  set /a startLine=%lines% - 1
  more /e +%startLine% someFile.txt &gt; lastLines.txt
  </code></pre>
  
  <p>The above code works for one file at a time. What I need is to read the last line from all files in a known list and add this line to a new .csv file.</p>
  
  <p>I have been using the following code for getting the 4th entry in the logfiles but it returns every line of every logfile: </p>
  
  <pre><code>for /f %%x in (%list%) do for /f "delims=.txt,  tokens=4" %%i in (%%x.txt) do echo %%x, %%i &gt;&gt; output.csv
  </code></pre>
  
  <p>What I would need is a sort of combination of both but I don't know how to combine them and make the complete last line be copied to the .csv file.</p>
  
  <p>===
  @Magoo:
  Thanx for your reaction.
  In every logfile can be 1 to >100 lines with comma separated information. Something like:</p>
  
  <pre><code>"LOGON,6-1-2015,12:43:39,USERNAME,HOSTNAME,,,,192.168.209.242,00:21:5A:2E:64:5E" 
  </code></pre>
  
  <p>The last code with the 4th entry was used to get a list of all accounts that had logged in to the computers. This code gave me a very large list of all logon/logoff events on all computerlogs I checked in %list%. 
  In %list$ I had all the names of logfiles I wanted to be checked. This returned all lines.</p>
  
  <p>For a new batchfile I need only the last logon/logoff entry and I want the whole last line. </p>
  
  <p>So I have a .txt file with the hostnames of all computers I need to examine. 
  This .txt file will be read line by line via the variable %list%.</p>
  
  <p>From every logfile I need only the last line copied to an output file. </p>
  
  <p>===
  I just tried the solution offered by JosefZ. Unfortunately this does not work for me yet. No lastlines are copied to the resultfile. In the code I removed the extra entry for possible lastlines for there are no empty lines in the logs, I also added an entry for the hostname I want to be available in the result. JosefZ had the filename there:</p>
  
  <pre><code>@ECHO OFF &gt;NUL
  @SETLOCAL enableextensions disabledelayedexpansion    
  type nul&gt;output.csv    
  set "list=_listing.txt"
  for /F "tokens=*" %%x in ('type "%list%"') do (
    set "host=%%~x" 
    for /F "tokens=*" %%G in ('type "%%~x"') do set "lastline=%%G"
    call :lline
  )    
  :endlocal
  @ENDLOCAL
  goto :eof    
  :lline    
    set "filename=.\logs\%filename:&amp;=^&amp;%.txt"
    echo %host%,%lastline%&gt;&gt;output.csv
  goto :eof
  </code></pre>
  
  <p>The resultfile shows only the hostnames. I'll puzzle some more with this but all tips are welcome!</p>
  
  <p>===
  Got it!!!</p>
  
  <pre><code>@ECHO OFF &gt;NUL
  @SETLOCAL enableextensions disabledelayedexpansion
  
  type nul&gt;output.csv
  
  set "list=_listing.txt"
  for /F "tokens=*" %%x in ('type "%list%"') do (
    set filename=   :: *empty previous filename*
    set lastline=   :: *empty previous lastline* 
    set "host=%%~x" 
    set "filename=.\logs\%host%.txt"  :: *creating the filename from path+hostname+extention*
    for /F "tokens=*" %%G in ('type "%filename%"') do set "lastline=%%G"
    call :lline
  )
  
  :endlocal
  @ENDLOCAL
  goto :eof
  
  :lline
  
    echo %host%,%lastline%&gt;&gt;output.csv
  
  goto :eof
  </code></pre>