Solved

Search Text file for Date

Posted on 1998-05-08
17
197 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
ID: 1343307
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
ID: 1343308
Woops, I believe it has to be (Pos('~',LineStr)-1)
0
 
LVL 3

Author Comment

by:d4jaj1
ID: 1343309
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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

 
LVL 8

Expert Comment

by:ZifNab
ID: 1343310
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
ID: 1343311
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
ID: 1343312
Hi,
When do you get these errors? And where?
ZiF.
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1343313
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
ID: 1343314
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
 
LVL 8

Expert Comment

by:ZifNab
ID: 1343315
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
ID: 1343316
Mmm, strange indeed, What if you use DeleteFile?
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1343317
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
ID: 1343318
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
ID: 1343319
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
ID: 1343320
And thus this solves the problem?
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1343321
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
ID: 1343322
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
ID: 1343323
Thanks, everything works now.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Swiper slider stops between pages 11 51
Fixed div within Bootstrap carousel item 11 61
Graphics32 under Delphi 10.1 Berlin 2 105
Drag & Drop... Data from one grid to another 2 42
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…
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

820 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