• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 210
  • Last Modified:

Search Text file for Date

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
d4jaj1
Asked:
d4jaj1
  • 11
  • 6
1 Solution
 
ZifNabCommented:
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
 
ZifNabCommented:
Woops, I believe it has to be (Pos('~',LineStr)-1)
0
 
d4jaj1Author Commented:
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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
ZifNabCommented:
Oops, don't forget to delete the source file at the end, and again to rename the new file...
0
 
ZifNabCommented:
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
 
d4jaj1Author Commented:
Hi,
When do you get these errors? And where?
ZiF.
0
 
ZifNabCommented:
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
 
d4jaj1Author Commented:
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
 
ZifNabCommented:
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
 
d4jaj1Author Commented:
Mmm, strange indeed, What if you use DeleteFile?
0
 
ZifNabCommented:
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
 
ZifNabCommented:
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
 
d4jaj1Author Commented:
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
 
ZifNabCommented:
And thus this solves the problem?
0
 
ZifNabCommented:
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
 
d4jaj1Author Commented:
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
 
ZifNabCommented:
Thanks, everything works now.
0

Featured Post

Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

  • 11
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now