Delete last line from text file using a Windows script?

Hi experts,

dooes anyone know of an easy way to delete the last line of a text file using a cmd or powershell script?

I've checked this page for command prompt. But it seems lots of work.
http://stackoverflow.com/questions/611796/how-can-i-delete-the-last-line-of-a-text-file-using-the-command-prompt

I'm looking into powershell, but it seems I have to spend hours to learn how to use it.

What do you think?

Thanks.
nvs_victorAsked:
Who is Participating?
 
soostibiCommented:
If the file is not very big:

$path = "C:\yourfile.txt"

$file = Get-Content $path -ReadCount 0
Set-Content $path -Value ($file | Select-Object -First ($file.count-1))

Open in new window

1
 
oBdACommented:
Batch if there are no empty lines or you don't care about them (they will be lost with this one):
@echo off
setlocal enabledelayedexpansion
set InFile=test.txt
set OutFile=test-nolastline.txt
if exist "%OutFile%" del "%OutFile%"
set Line=
for /f "delims=" %%a in ('type "%InFile%"') do (
  if not "!Line!"=="" >>"%OutFile%" echo.!Line!
  set Line=%%a
)

Open in new window

This version will keep empty lines:
@echo off
setlocal enabledelayedexpansion
set InFile=test.txt
set OutFile=test-nolastline.txt
if exist "%OutFile%" del "%OutFile%"
set Line=__DUMMY__
for /f "tokens=1* delims=]" %%a in ('type "%InFile%" ^| find /n /v ""') do (
  if not "!Line!"=="__DUMMY__" >>"%OutFile%" echo.!Line!
  set Line=%%b
)

Open in new window

0
 
nvs_victorAuthor Commented:
It worked!!!!

It took me a while to find out how to create a powershell file by their extensions... something so simple  so hard to find on google!

Thanks!
0
Simplify Active Directory Administration

Administration of Active Directory does not have to be hard.  Too often what should be a simple task is made more difficult than it needs to be.The solution?  Hyena from SystemTools Software.  With ease-of-use as well as powerful importing and bulk updating capabilities.

 
nvs_victorAuthor Commented:
Sorry oBdA, it didn't work. I'll try again tomorrow.
0
 
oBdACommented:
What exactly didn't work? Neither script will create any console output, they will just create the file defined in the variable "OutFile".
0
 
nvs_victorAuthor Commented:
I know, it works on the Outfile.

I ran out of time today again :(
I'll check tomorrow.

Thanks!
0
 
nvs_victorAuthor Commented:
oBdA, it worked!!!

Thanks!

I know why it didn't work the first time. I tried putting the address in quotes, like this:  

set InFile="C:\Users\name\Desktop\19 FTP automated scripts\test.txt"

And that caused it to fail. Command language is the only one I know that doesn't accept double quotes for file paths!

Thanks again!
0
 
nvs_victorAuthor Commented:
Thanks experts!
0
 
oBdACommented:
Oh, batch does accept double quotes; but here, the quotes are added during the execution of the commands ('type "%InFile%"').
0
 
nvs_victorAuthor Commented:
Thanks for the batch solution, as it turned out that  powershell was not installed on Windows Server 2003. :)


0
 
nvs_victorAuthor Commented:

"Oh, batch does accept double quotes; but here, the quotes are added during the execution of the commands ('type "%InFile%"')."

Oh, I see. No wonder why it didn't work.

Thanks  for clarifying it for me. :)
0
 
lala ningsihCommented:
thank you for your post guys.

It works also for me.
But, my file size is bigger than 20 MB. When I execute the bat program. the result is nothing.

If there are any problem?.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.