Re: How Can I Delete blank lines from a Text file? by Rafael
Rafael
Sun Dec 02 16:59:18 PST 2007
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.r.felkins@wellsfargo.com> wrote in message
news:71c8b812-223b-4d5a-a4ab-b3a210a4d7f9@b40g2000prf.googlegroups.com...
> 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
>>
>> news:ced0175c-5d3d-4b45-8c3c-6fa594c09bc4@t47g2000hsc.googlegroups.com...
>>
>>
>>
>> > 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- 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
> Loop
>
> objFile.Close
>
> Set objFile = objFSO.OpenTextFile(strFile, ForWriting)
> objFile.Write strContents
>
> objFile.Close
> End If
> Next