Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi! I would like to use a batch file to delete the last line of a text file and send the rest of the file to a new text file. I have duplicates rows in my file and need to delete only the last line.
Thank You

@echo off & setlocal EnableDelayedExpansion > file.new set /P row=< file.txt for /F "skip=1 delims=" %%j in (file.txt) do ( echo.!row!>> file.new set row=%%j )

@IVO, the problem with your solution is that blank lines are not preserved.
egC:\test>more file one two three four five last C:\test>test.bat one two three four five@OP, if not a big file, you can use this vbscript
Set objFS=CreateObject("Scripting.FileSystemObject") strFile = "c:\test\file" Set objFile = objFS.OpenTextFile(strFile) arr=Split(objFile.ReadAll,vbCrlf) For i=LBound(arr) To UBound(arr)-2 WScript.Echo arr(i) Next objFile.Close

@ghostdog
I am aware of the issue you point out, but I have noticed rarely the posters care of that problem, so I avoid to set up more complex solutions to prevent the loss of blank lines, leaving that code for explicit requests.

Or, if you want an option that is more concise, here's a Perl solution.
This edits the existing file
c:\test>perl -ni.bak -e "print if ! eof" fileThis creates a new file
c:\test>perl -ne "print if ! eof" file > newfile

![]() |
![]() |
![]() |
| Login or Register to Reply | |
| Login | Register |
| Ads by Google |