Solved

Search Text file for Date

Posted on 1998-05-08
17
200 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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

Are You Using the Best Web Development Editor?

The worlds of web hosting and web development are constantly evolving. Every year we see design trends change, coding standards adapt and new frameworks/CMS created. With such a quick pace of change it’s easy to get lost trying to keep up.

See if your editor made the list.

Question has a verified solution.

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

Preface In the first article: A Better Website Login System (http://www.experts-exchange.com/A_2902.html) I introduced the EE Collaborative Login System and its intended purpose. In this article I will discuss some of the design consideratio…
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
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:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

628 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