Solved

Search Text file for Date

Posted on 1998-05-08
17
194 Views
Last Modified: 2013-11-18
Hello,

I have a text file that contains rows of data separted by a ~, with each row having 3 columns.  The first column has a max of 8 charaters, the second 25 and the last 255 max.

date~title~message

How do I scroll through the text file (the first column really) and find the date that matches today's date, displays it in a message box, then deletes the entire row (along with blank line).  I already know how to use the now abd strtodate functions, I'm just not good at using I/O routines and text file maipulation.  I've included what I've come up with so far (which could be totally wrong).  Finally, please don't give any answers about the BDE - the users doesn't have it installed.

procedure TForm1.Button1Click(Sender: TObject);
var
  s:  string;
  f1: textfile;
begin
 AssignFile(F1, 'extractfilepath(application.exename) + 'textfile.txt');
 Reset(F1);
 while not Eof(F1) do
  begin
   Readln(s, ????);   // maybe read ???
   if {strtodate (charaters before 1st ~)} = date then
    begin
     {take charaters after 3rd/last ~ as string to go into showmessage)
     {delete entire line & remove white space}
    end;
  end;
 CloseFile(F1);
 end;
end;
0
Comment
Question by:d4jaj1
  • 11
  • 6
17 Comments
 
LVL 8

Accepted Solution

by:
ZifNab earned 50 total points
Comment Utility
Hi d4jaj1,


Readln is fine... it's reads to the end of the line...

var LineStr : String;
     DateStr : String;

So :

 Readln(F1, LineStr);
 DateStr := Copy(LineStr, 0, Pos('~',LineStr);
 if StrToDate(DateStr)  = date then
  begin
   ..............
  end;

Regards, ZiF.

 
   
     
0
 
LVL 8

Expert Comment

by:ZifNab
Comment Utility
Woops, I believe it has to be (Pos('~',LineStr)-1)
0
 
LVL 3

Author Comment

by:d4jaj1
Comment Utility
Hi d4jaj1,

I propose this :

 One file as source,
 to the other you copy all the lines which don't have to be deleted (Writeln(LineStr)-.. (I   think this is the best way to do it)

 To get the next '~', just delete everything before the first '~' and '~' itself, search again for '~' , etc, ...

 Delete(LineStr, 0, Pos('~',LineStr));

Just bring it in a loop and of you go...

Regards, Zif.
0
 
LVL 8

Expert Comment

by:ZifNab
Comment Utility
Oops, don't forget to delete the source file at the end, and again to rename the new file...
0
 
LVL 8

Expert Comment

by:ZifNab
Comment Utility
Can't get your suggestion of two files to work.  I get I/O errors of 102 or 104.  Isn't there an easier way.

procedure TForm1.Button3Click(Sender: TObject);
var
 LineStr,DateStr,S,
 titlestr, msgstr : String;
 f1,f2: textfile;
begin
S := Application.Title;
try
 AssignFile(F1, extractfilepath(application.exename) + 'textfile.txt');
 Reset(F1);
except
 begin
  AssignFile(F1, extractfilepath(application.exename) + 'textfile.txt');
  rewrite(f1);
 end
end;
AssignFile(F2, 'temp.$$$');
Rewrite(F2);
try
 while not EOF(f1) do begin
  Readln(F1, LineStr);
  DateStr := Copy(LineStr, 0, (Pos('~',LineStr)-1));
  if datestr <> '' then
   if StrToDate(DateStr) = date then
    begin
     TitleStr := Copy(Linestr, 10, (Pos('~~',LineStr)-10));
     msgstr := copy(linestr, (Pos('~~',LineStr)+2), 255);
     Application.Title := titlestr;
     showmessage(msgstr);
    end
   else
    begin
     Writeln(F2, linestr);
    end;
  end;
 closefile(f1);
 erase(f1);
 rename(f2, 'textfile.txt');
finally
 Application.Title := S;
 CloseFile(F2);
end;
end;

0
 
LVL 3

Author Comment

by:d4jaj1
Comment Utility
Hi,
When do you get these errors? And where?
ZiF.
0
 
LVL 8

Expert Comment

by:ZifNab
Comment Utility
Two places.  If the file (textfile) doesn't already exist - in the While not block after readln.  In the file already exists, on the rename line.
0
 
LVL 3

Author Comment

by:d4jaj1
Comment Utility
Hi d4jaj1,

1. The first error is normal, because if file F1 doesn't exist, you create one F1 (REWRITE).
    AND when you read F1 (just created), you offcourse read nothing....

So you should do :

try
        AssignFile(F1, extractfilepath(application.exename) + 'textfile.txt');
        Reset(F1);
       except
        begin

        {PLACE HERE YOUR CODE OF READING F1 AND SAVING TO F2}

        end
       end;

 Second is also normal, because you can't rename a file to a file which already exist, this correct too because otherwise we should lose valueble files.... If the file already exists, you should popup a file-dialog asking that the user fill in a name for the file...
ZIF.

0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 8

Expert Comment

by:ZifNab
Comment Utility
But that's just it, the file doesn't already exist.  Right before the rename, erase(f1) executes and was successful (it's gone from my hard drive).  Do I need to remove it from memory?
0
 
LVL 3

Author Comment

by:d4jaj1
Comment Utility
Mmm, strange indeed, What if you use DeleteFile?
0
 
LVL 8

Expert Comment

by:ZifNab
Comment Utility
d4jaj1,

just thought of something : F2 on which directory do you create it?

This it should be :

AssignFile(F2, extractfilepath(application.exename) + 'temp.$$$');

Zif.
0
 
LVL 8

Expert Comment

by:ZifNab
Comment Utility
I didn't specify a directory, but it created the file in the existing directory.  I changed it just to see, but it didn't work.
0
 
LVL 3

Author Comment

by:d4jaj1
Comment Utility
hi d4jaj1, hehe, I 'm working too long today... hehe, I guess you too.... we rename a file before it's  closed... hehe ... not so good I guess... What about first closing F2 and then renaming it? I think that would be better...
0
 
LVL 8

Expert Comment

by:ZifNab
Comment Utility
And thus this solves the problem?
0
 
LVL 8

Expert Comment

by:ZifNab
Comment Utility
Tried it, and it works as long as teh file exists.  Ithought rewrite was supposed to create the file if it didn't exist?  It also crashes if the txt file is empty.  O well - its Friday.  I'll tackle it again Monday - maybe over the weekend.  Thanks.

I guess my DLL question wasn't so easy afterall.  Know one has answered it on the Delphi newsgroup either.
0
 
LVL 3

Author Comment

by:d4jaj1
Comment Utility
Hi d4jaj1,

 What's still going wrong?
   
 -  Rewrite should work (create's the file if it doesn't already exist, BUT it doesn't create the nessecary directories!)
 - Crashing with empty file? Haven't we discussed this :  Friday, May 08 1998 - 10:15AM PDT
   You should check on empty files... Use {$I-} {$I+} and IOERROR.
 
 Is the renaming solved with the above proposals? Friday, May 08 1998 - 10:37AM PDT

Regards, Zif.
0
 
LVL 8

Expert Comment

by:ZifNab
Comment Utility
Thanks, everything works now.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Shoutout to Emily Plummer (http://www.experts-exchange.com/members/eplummer26.html) for giving me this article! She did most of it, I just finished it up and posted it for her :)    Introduction In a previous article (http://www.experts-exchang…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now