Delphi delete text file




















Deleting a file erases the file from the disk and removes the entry from the disk's directory. There is no corresponding operation to restore a deleted file, so applications should generally allow users to confirm deletions of files. The application now deletes the selected file in response to either FilelDelete or the Del key, which is the shortcut for that menu item.

Mark van Heerden Mark van Heerden 4 4 silver badges 14 14 bronze badges. I guess you have to loop through each line of sl and compare it. When you found a match remember the index your on and pass it to sl.

Delete — GuidoG. You are not asking about deleting a line. You already know how to do that. You are asking about finding a line. Read each line, parse it, find the line s that match. What part can't you do?

The term Index, in this context, refers to the Line Number of the Item within the list. TStringLists are 0 based, so the first line is 0, 2nd is 1, etc. The answers below help you, and can be modified to suit your own circumstances. Also, be aware there is a bug that causes Delimiters to not function correctly certainly in Delphi 7 , don't rely on it. Add a comment.

Active Oldest Votes. SaveToFile 'filename' ; end; sl. Re0sless Re0sless This algorithm works, but once the line has been deleted it leaves an extra open line in the text file which is problematic when it comes to populating an array of users from data in the text file. Is there any way to execute this algorithm but incorporate a method to delete the extra line that is left in the text file?

MarkvanHeerden: What extra line are you referring to? Please be more specific. Delete removes the entire line, it does not write it out as an empty line.

The only possible place I could think an empty line would appear is at the very end of the file, but SaveToFile should not be writing an empty line there, unless the last entry in the list is empty to begin with. Are you SURE no entries in the list are empty to begin with? The empty line that appears is at the end of the file.

And you are correct, the last entry is an empty line to begin with. This is due to when adding a new user I use the Writeln statement and add a 13 at the end to ensure that it writes to a new line every time.

Is there a way i can rework to avoid getting the extra line? MarkvanHeerden simply do not append a 13 manually. WriteLn already outputs a line break for you. Create; sl. LoadFromFile 'filename' ; sl. Delete Index ; sl. SaveToFile 'filename' ; sl. Thanks Theo, i know that method also.

But is it possible to delete a line in a text file, directly, without using a stringgrid or something else?

Troodon Sr. Member Posts: You could try running external commands sed, grep, etc. Quote from: fredvs on December 09, , pm. Quote from: theo on December 09, , pm. IOUtils and TDirectory would work too. At least this approach avoids all the heap allocation of IOUtils, a historically bug ridden unit fwiw — David Heffernan. It frees up the resources used by the search in SearchResults. A findclose call is not necessary if FindFirst finds nothing, but must be called if it does, even if a subsequent FindNext call fails.

Magoo stackoverflow. Show 1 more comment. Delete FileName ; end;. Andreas Andreas 4 4 silver badges 5 5 bronze badges. Why is it bad to perform this in a single routine? Your approach involves two separate enumerations which seems like a bad idea to me.

I read the OP as both requirements have to be met match a mask AND be older than some days , so you can't split the search in two routines, you need to check both conditions on the same routine. Yes, both requirements have to be met, which is done actually. Default value for DaysOld and the related comment in the OP's answer make it seem like there is additional intention for the routine - deleting files by mask.



0コメント

  • 1000 / 1000