?
Solved

Delete last line from text file using a Windows script?

Posted on 2011-05-11
12
Medium Priority
?
7,012 Views
Last Modified: 2016-11-20
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.
0
Comment
Question by:nvs_victor
12 Comments
 
LVL 13

Accepted Solution

by:
soostibi earned 1000 total points
ID: 35742312
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
 
LVL 85

Assisted Solution

by:oBdA
oBdA earned 1000 total points
ID: 35742391
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
 

Author Comment

by:nvs_victor
ID: 35742432
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
What Security Threats Are We Predicting for 2018?

Cryptocurrency, IoT botnets, MFA, and more! Hackers are already planning their next big attacks for 2018. Learn what you might face, and how to defend against it with our 2018 security predictions.

 

Author Comment

by:nvs_victor
ID: 35742456
Sorry oBdA, it didn't work. I'll try again tomorrow.
0
 
LVL 85

Expert Comment

by:oBdA
ID: 35742490
What exactly didn't work? Neither script will create any console output, they will just create the file defined in the variable "OutFile".
0
 

Author Comment

by:nvs_victor
ID: 35750503
I know, it works on the Outfile.

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

Thanks!
0
 

Author Comment

by:nvs_victor
ID: 35754694
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
 

Author Closing Comment

by:nvs_victor
ID: 35754706
Thanks experts!
0
 
LVL 85

Expert Comment

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

Author Comment

by:nvs_victor
ID: 35756878
Thanks for the batch solution, as it turned out that  powershell was not installed on Windows Server 2003. :)


0
 

Author Comment

by:nvs_victor
ID: 35757065

"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
 

Expert Comment

by:lala ningsih
ID: 41895458
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

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
In the absence of a fully-fledged GPO Management product like AGPM, the script in this article will provide you with a simple way to watch the domain (or a select OU) for GPOs changes and automatically take backups when policies are added, removed o…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Loops Section Overview
Suggested Courses
Course of the Month15 days, 3 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question