Sign in to follow this  
Followers 0
AGsol

Delete a specific line from a Text File

9 posts in this topic

#1 ·  Posted (edited)

Hi All

I need some help figuring out how to remove a line from a text file but not leaving a blank line.

Currently this is happening

Example Text File:

Blah#Blah#Blah#

Blah2#Blah2#Blah2#

Blah3#Blah3#Blah3#

Blah4#Blah4#Blah4#

Blah5#Blah5#Blah5#

Text File after changes:

Blah#Blah#Blah#

Blah2#Blah2#Blah2#

Blah3#Blah3#Blah3#

Blah5#Blah5#Blah5#

I want this to happen

Example Text File:

Blah#Blah#Blah#

Blah2#Blah2#Blah2#

Blah3#Blah3#Blah3#

Blah4#Blah4#Blah4#

Blah5#Blah5#Blah5#

Text File after changes:

Blah#Blah#Blah#

Blah2#Blah2#Blah2#

Blah3#Blah3#Blah3#

Blah5#Blah5#Blah5#

I want it to append to the same file.

CODE
Func Delete()

$Test = GUICtrlRead($ListView1, BitOR($LVS_EX_BORDERSELECT,$LVS_EX_ONECLICKACTIVATE))

If $Test = "Prompt" Then

Else

$maybe = FileReadLine("c:\Software.txt", $Test -9)

$replace = ("")

_ReplaceStringInFile("c:\Software.txt",$maybe,$replace)

MsgBox(0,"Deleted","An Entry has been deleted, The list will now refresh. You should now update the Database")

GUIDelete()

Run("C:\AI\AI.exe")

Exit

EndIf

EndFunc

Hope someone can help

Edited by Heptinstall

Share this post


Link to post
Share on other sites



Before anyone mentions it I know that i'm using replace at the moment but I wanted you to see exactly what I was putting to get my current out put.

I believe using an array and writing the file back using append option 2 works, but I can't figure that one out.

Share this post


Link to post
Share on other sites

#include <Array.au3>
#include <File.au3>


Dim $sFile = @ScriptDir & '\blah.txt'
Dim $aLines
_FileReadToArray($sFile, $aLines)
_ArrayDelete($aLines, $aLines[0]-1)
_ArrayDelete($aLines, 0)
_FileWriteFromArray($sFile, $aLines)


#cs
    ### blah.txt content: ###.
    
    Blah1#Blah1#Blah1#
    Blah2#Blah2#Blah2#
    Blah3#Blah3#Blah3#
    Blah4#Blah4#Blah4#
    Blah5#Blah5#Blah5#
#ce

?

Share this post


Link to post
Share on other sites

This doesn't work I still end up with a blank line but this time at the top.

Share this post


Link to post
Share on other sites

Forgot to say thanks for your help.

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

_ArrayReadFromFile creates a one-based array with the record count in the first element, and _ArrayDelete does not automatically update that count. So I'm thinking your for/next loop is going to blow up when you try to reference an element that no longer exists. You could probably trash the loop and use _ArrayReadFromFile's counterpart: FileWriteFromArray (as suggested in on of your first replies), then you won't need to manually keep track of how many elements remain. Something like:

#include <Array.au3>
#include <File.au3>

Dim $sFile = @ScriptDir & '\blah.txt'
Dim $aLines
_FileReadToArray($sFile, $aLines)
_ArrayDisplay($aLines)
If @error Then 
    MsgBox(0, "", "File does not exist or is empty")
    Exit
EndIf
Delete_Lines()
_FileWriteFromArray($sFile, $aLines, 1)
_ArrayDisplay($aLines)
Exit

Func Delete_Lines()
;   Do the voodoo that you do here  
    _ArrayDelete($aLines,3)
EndFunc

Edit: mangled my code tags

Edit2: I should have just pointed you back to Authenticity's post, it's the same thing. Apparently, you just needed the test in there that trashes blank lines (like your last example).

Edited by Spiff59

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0