Indigo Rose Software

Professional Software Development Tools

 
Results 1 to 10 of 10
  1. #1
    Join Date
    Oct 2004
    Posts
    6

    Delete blank lines from a text file

    Does anyone know if it is possible to remove blank lines from a text file?

    I am trying to remove entries from a hosts file and can delete the entry but it leaves a blank line. I would like to remove the blank lines to keep the hosts file tidy - any ideas?

  2. #2
    Join Date
    Jul 2002
    Location
    USA
    Posts
    3,959
    I didn't test this code, but it should work, or at least with minimal changes

    Code:
    --read the host file into a table
    tblHosts = TextFile.ReadToTable(_SystemFolder.."\\Drivers\\etc\\hosts")
    --if the table isn't empty
    if Table.Count(tbHosts) > 0 then
    	--start at the end of the file and loop through
    	--to remove all blank lines
    	for n=Table.Count(tblHosts), 1, -1 do
    		--trim spaces
    		sTemp = String.TrimLeft(tblHosts, " ")
    		if sTemp == "\r\n" then
    			Table.Remove(tblHosts, n)
    		end
    	end
    end

  3. #3
    Join Date
    Oct 2004
    Posts
    6
    Thanks for having a look at this but it doesn't seem to work. It fails at runtime with a message of "Argument 1 must be of type string".

    I think this is on the sTemp = String.TrimLeft(tblHosts, " ") line as tblhosts should be a string.

    Tried a few things but still stuck I'm afraid, any other ideas would be greatly appreciated.

  4. #4
    Join Date
    Jan 2000
    Posts
    2,002
    Make it:

    sTemp = String.TrimLeft(tblHosts[n], " ")

  5. #5
    Join Date
    Jul 2002
    Location
    USA
    Posts
    3,959
    Also:

    if Table.Count(tbHosts) > 0 then

    should be

    if Table.Count(tblHosts) > 0 then

    <SelfReminder>Test Code before Posting</SelfReminder>

  6. #6
    Join Date
    Jul 2002
    Location
    USA
    Posts
    3,959
    Okay, I actually tested this one

    Code:
    --read the host file into a table
    tblHosts = TextFile.ReadToTable(_SystemFolder.."\\Drivers\\etc\\hosts")
    --if the table isn't empty
    if Table.Count(tblHosts) > 0 then
    	--start at the end of the file and loop through
    	--to remove all blank lines
    	for n=Table.Count(tblHosts), 1, -1 do
    		--trim spaces
    		sTemp = String.TrimLeft(tblHosts[n], " ")
    		if sTemp == "" then
    			Table.Remove(tblHosts, n)
    		end
    	end
    end
    	
    TextFile.WriteFromTable(_SystemFolder.."\\Drivers\\etc\\hosts", tblHosts, false)

  7. #7
    Join Date
    Sep 2004
    Posts
    60
    Quote Originally Posted by Worm

    <SelfReminder>Test Code before Posting</SelfReminder>
    Air code will get you every time.

    That said it was an elegant solution

  8. #8
    Join Date
    Oct 2004
    Posts
    6
    Sure is, tested this and it worked a treat! Thanks alot for all your help.

  9. #9
    Join Date
    Jul 2002
    Location
    USA
    Posts
    3,959
    Quote Originally Posted by Absynthe
    Air code will get you every time.
    Funny. My Air Guitar is so much better than the real deal, why not my code? Another of life's mysteries I guess.

  10. #10
    Corey is offline Indigo Rose Staff Alumni
    Join Date
    Aug 2002
    Posts
    9,746
    Air guitar is more noble than air coding by a factor of three. Just an observation...

Similar Threads

  1. Example: Loading Paragraph Text Using a Timer
    By Jonas DK in forum AutoPlay Media Studio 5.0 Examples
    Replies: 7
    Last Post: 11-25-2004, 05:10 PM
  2. Reading Specific Lines from a Text File
    By Desmond in forum AutoPlay Media Studio 5.0 Examples
    Replies: 0
    Last Post: 09-26-2003, 10:38 AM
  3. Writing Text to a File
    By Desmond in forum AutoPlay Media Studio 5.0 Examples
    Replies: 0
    Last Post: 09-22-2003, 02:02 PM
  4. Getting lines from a text file....
    By RobbyH in forum AutoPlay Media Studio 4.0
    Replies: 3
    Last Post: 03-03-2003, 10:53 AM
  5. Replies: 0
    Last Post: 08-17-2000, 02:29 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts