AGsol Posted March 13, 2009 Share Posted March 13, 2009 (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. CODEFunc 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 March 13, 2009 by Heptinstall Link to comment Share on other sites More sharing options...
AGsol Posted March 13, 2009 Author Share Posted March 13, 2009 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. Link to comment Share on other sites More sharing options...
Authenticity Posted March 13, 2009 Share Posted March 13, 2009 #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 ? Link to comment Share on other sites More sharing options...
AGsol Posted March 13, 2009 Author Share Posted March 13, 2009 This doesn't work I still end up with a blank line but this time at the top. Link to comment Share on other sites More sharing options...
PhilRip Posted March 13, 2009 Share Posted March 13, 2009 #include <Array.au3> #include <File.au3> Dim $sFile = @ScriptDir & '\blah.txt' Dim $aLines _FileReadToArray($sFile, $aLines) If @error Then MsgBox(0, "", "File does not exist") Exit EndIf FileDelete($sFile) For $i=1 to $aLines[0] If $aLines[$i]<>"" Then FileWriteLine($sFile,$aLines[$i]) Next Link to comment Share on other sites More sharing options...
AGsol Posted March 13, 2009 Author Share Posted March 13, 2009 #include <Array.au3> #include <File.au3> Dim $sFile = @ScriptDir & '\blah.txt' Dim $aLines _FileReadToArray($sFile, $aLines) If @error Then MsgBox(0, "", "File does not exist") Exit EndIf FileDelete($sFile) For $i=1 to $aLines[0] If $aLines[$i]<>"" Then FileWriteLine($sFile,$aLines[$i]) Next I can't see how this removes a line? Link to comment Share on other sites More sharing options...
AGsol Posted March 13, 2009 Author Share Posted March 13, 2009 (edited) #include <Array.au3> #include <File.au3> Dim $sFile = "c:\AI\blah.txt" Dim $aLines _FileReadToArray($sFile, $aLines) _ArrayDelete($aLines,4) FileDelete($sFile) For $i=1 to $aLines[0] If $aLines[$i]<>"" Then FileWriteLine($sFile,$aLines[$i]) Next This works enough for me. Edited March 13, 2009 by Heptinstall Link to comment Share on other sites More sharing options...
AGsol Posted March 13, 2009 Author Share Posted March 13, 2009 Forgot to say thanks for your help. Link to comment Share on other sites More sharing options...
Spiff59 Posted March 13, 2009 Share Posted March 13, 2009 (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 March 13, 2009 by Spiff59 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now