Batch file to return to the last line of a text file

I have a file that contains the result of comparing files written to a text file:
Comparing files C:\LOGS\old.txt and C:\LOGS\NEW.TXT
***** C:\LOGS\old.txt
***** C:\LOGS\NEW.TXT
folder_thats_different
*****

I need to output the following last line "folder_thats_different" and insert a new line:
folder contains a file that is different: folder_thats_different

Yes, I know that I can use a different language, but now I'm stuck in batch files.

+4
source share
3 answers

You can try to read it with a for loop and take the current line and always keep the previous line

 @echo off setlocal EnableDelayedExpansion for /f "delims=" %%x in (myFile.txt) do ( set "previous=!last!" set "last=%%x" ) echo !previous! 
+3
source

Here is an example that you can use as a starting point. Just change the file name in the set command= to the appropriate name (or replace the command with what will rename the log listing).

 @echo off @setlocal (set command=type test.txt) for /f "usebackq tokens=*" %%i in (`%command%`) do call :process_line %%i echo next to last line: %old_line% goto :eof :process_line (set old_line=%new_line%) (set new_line=%*) goto :eof 

Of course, you probably want to do something other than just repeat the found line.

0
source

The first answer works for me. I also added 2 lines after the end so that it repeats so that I can observe the active log file without closing or opening it again. I do a lot of debugging for the mods that are used in the game Space Engineers. My version looks like this:

 @echo off setlocal EnableDelayedExpansion for /f "delims=" %%x in (SpaceEngineers.log) do ( set "previous=!last!" set "last=%%x" ) echo !previous! timeout 15 /nobreak se_log 

The line below stops the batch file interleaving too fast and stops the key bypass. To change the time in seconds, simply change the number "15" to whatever you want. To stop the batch file, just press ctrl + c .

 timeout 15 /nobreak 

The line below is the name of the batch file that I made it tell CMD to run again.

 se_log 
0
source

All Articles