Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How Can I Delete blank lines from a Text file?

1,105 views
Skip to first unread message

OldDog

unread,
Nov 27, 2007, 6:24:12 PM11/27/07
to
Hi,

I have a text file with hundreds of blank lins scatered through out
the file. How Can I Delete the blank lines.

I found this script that will delete the first 5 lines of a file, so I
am guesing I could use it and Test for a blank line.

What would the Test be?

Set objFSO = CreateObject("Scripting.FileSystemObject")

strComputer = "."

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root
\cimv2")

Set colFiles = objWMIService.ExecQuery _
("ASSOCIATORS OF {Win32_Directory.Name='C:\Scripts'} Where " _
& "ResultClass = CIM_DataFile")

For Each objLogFile In colFiles
If objLogFile.Extension = "txt" Then
strFile = objLogFile.Name
Set objFile = objFSO.OpenTextFile(strFile, ForReading)

i = 1

Do Until objFile.AtEndOfStream
strLine = objFile.ReadLine
If i > 5 Then
strContents = strContents & strLine & vbCrLf
End If
i = i + 1
Loop

objFile.Close

Set objFile = objFSO.OpenTextFile(strFile, ForWriting)
objFile.Write strContents

objFile.Close
End If
Next

Rafael T

unread,
Nov 27, 2007, 9:02:54 PM11/27/07
to
OldDog,

the trick is easy. you actually do not need to delete the lines of the
document but create a new one without the empty lines.

You have everything you need in the code you posted. The only new thing
needed is a new file to write to.

Here is some pseudo code to help you.
open file A for reading
open file B for writing

Start loop until end of file A
read one line from A
if line is NOT empty
write line to B
end of loop

hope this helps, if you need more help don't hesitate to post again.

--
**********
Rafael T


"OldDog" <michael....@wellsfargo.com> wrote in message
news:ced0175c-5d3d-4b45...@t47g2000hsc.googlegroups.com...

OldDog

unread,
Nov 28, 2007, 1:24:27 PM11/28/07
to
On Nov 27, 8:02 pm, "Rafael T" <okinawa...@hotmail.com> wrote:
> OldDog,
>
> the trick is easy. you actually do not need to delete the lines of the
> document but create a new one without the empty lines.
>
> You have everything you need in the code you posted. The only new thing
> needed is a new file to write to.
>
> Here is some pseudo code to help you.
> open file A for reading
> open file B for writing
>
> Start loop until end of file A
> read one line from A
> if line is NOT empty
> write line to B
> end of loop
>
> hope this helps, if you need more help don't hesitate to post again.
>
> --
> **********
> Rafael T
>
> "OldDog" <michael.r.felk...@wellsfargo.com> wrote in message
> > Next- Hide quoted text -
>
> - Show quoted text -

Hi,

Thanks for the responce. Here is what I have so far, and it works to
an extent.
However, It turns out that not all the lines that I thought were
empty, are empty.
They start with a TAB charactor. Is there a way to detect if the new
line only has a TAB
charactor and skip that?

Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8

Set objFSO = CreateObject("Scripting.FileSystemObject")

strComputer = "."

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root
\cimv2")

Set colFiles = objWMIService.ExecQuery _

("ASSOCIATORS OF {Win32_Directory.Name='Y:\'} Where " _


& "ResultClass = CIM_DataFile")

For Each objLogFile In colFiles
If objLogFile.Extension = "txt" Then
strFile = objLogFile.Name

WScript.Echo strFile


Set objFile = objFSO.OpenTextFile(strFile, ForReading)

Do Until objFile.AtEndOfStream
strLine = objFile.ReadLine
If strLine <> "" Then
strContents = strContents & strLine & VbCrLf
'WScript.Echo strContents
End If

Rafael T

unread,
Dec 2, 2007, 7:59:18 PM12/2/07
to
OldDog,

the ASCII number for tab is 9, maybe you can use it to identify the line.

if strLine=chr(9) then

hope this helps,
--
**********
Rafael T


"OldDog" <michael....@wellsfargo.com> wrote in message

news:71c8b812-223b-4d5a...@b40g2000prf.googlegroups.com...

0 new messages