Samar
Samar

Reputation:

How can I delete the last line of a text file using the command prompt?

I have a text file that I am importing into access table using command prompt. The problem is that this text file has a blank line as the last line. Can anyone provide me with a script that deletes my blank line at the end of file so as I can finalize my automation process. I am using Windows 2000 platform.

Upvotes: 4

Views: 14001

Answers (3)

Renze de Waal
Renze de Waal

Reputation: 533

 sed "$d" input.txt > output.txt

deletes the last line, but it's not provided by Windows (2000, XP, 7). Get the gnu utils to obtain a sed for Windows.

On this site you will find a list of other sed commands to manipulate files.

Upvotes: 4

notandy
notandy

Reputation: 3330

Here is a script that deletes blank lines from a text file. It is written for windows 2000.

Const ForReading = 1
Const ForWriting = 2

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\Scripts\Test.txt", ForReading)

Do Until objFile.AtEndOfStream
    strLine = objFile.Readline
    strLine = Trim(strLine)
    If Len(strLine) > 0 Then
        strNewContents = strNewContents & strLine & vbCrLf
    End If
Loop

objFile.Close

Set objFile = objFSO.OpenTextFile("C:\Scripts\Test.txt", ForWriting)
objFile.Write strNewContents
objFile.Close

Upvotes: -1

Joachim Sauer
Joachim Sauer

Reputation: 308061

This works using head

head -n -1 input.txt > output.txt

Unfortunately I think Windows doesn't come with that useful tool by default, but installing Cygwin gives you that and a lot of other stuff as well.

Upvotes: 3

Related Questions