Recent

Author Topic: Finding a file, only  (Read 890 times)

stephanos

  • New Member
  • *
  • Posts: 38
Finding a file, only
« on: July 11, 2022, 02:37:59 pm »
I have been experimenting with my PlayList Writer programme and observed a bug.  I was previously using findfirst onClick to check if the playlist file was present:
Code: Pascal  [Select][+][-]
  1. var
  2.      searchResult : TSearchRec;  // Test File
  3. if findfirst('PlayListStephen.m3u', faAnyFile, searchResult) = 0 then //
  4.  
  5. // after the if
  6. FindClose(searchResult);   //

On the occasions when the specified file was not present, its absence was detected, and the else part of the code, not shown here, worked.  Well that sounds all right. But, I only recently realised that as a result of not finding the file, it created one.  Which is not the intention.

I have experimented with the next approach, if FileIsInDirectory.  But I think I must have the coding wrong because
  • when the file is present it is detected – no problems, but when I delete the file and run the programme again
    the file is detected, even though it is not there. 
  • Then a new file is made.   Which is not the intention

In order to use ‘FileIsInDirectory’ I added LazUtils to the Project Inspector and added FileUtil to Uses:
Here is the code for the onClick event to test the file is present, and of course the target file sits in the same folder as all the other project files: 
Code: Pascal  [Select][+][-]
  1. procedure TForm1.BitBtn2Click(Sender: TObject);  // Test File
  2. begin
  3.      if FileIsInDirectory('PlayListStephen.m3u', 'C:\Users\stephanos\MyFiles\Delphi(Lazarus)\
  4.     PlayListV24-TimeLimit-FileIsInDirectory\') then
  5.      begin //
  6.        BitBtn3.Enabled := True;
  7.        Form1.ActiveControl := BitBtn3;
  8.        Open.Enabled := True;
  9.        Label3.Enabled := True;
  10.        Label3.Font.Color := clMaroon;
  11.        Label3.Caption := 'File present.  Click the Open File button to continue '#13#10' or                                                    press ALT + F to continue';
  12.      end
  13.      else
  14.      Begin
  15.      Label3.Caption := 'File NOT present'; //begin
  16.      end;   // End of if FileIsInDirectory('Play …….
  17.      Test.Enabled := False; BitBtn2.Enabled := False;
  18. end;

So I really do not know what I am doing.  Is there a way to check a file is present and simply do noting else except return a value that can be checked, with no other activity?

Thanks and wait to hear

dsiders

  • Hero Member
  • *****
  • Posts: 1079
Re: Finding a file, only
« Reply #1 on: July 11, 2022, 03:56:39 pm »
Code: Pascal  [Select][+][-]
  1. if findfirst('PlayListStephen.m3u', faAnyFile, searchResult) = 0 then //
  2.  

Code: Pascal  [Select][+][-]
  1. procedure TForm1.BitBtn2Click(Sender: TObject);  // Test File
  2. begin
  3.      if FileIsInDirectory('PlayListStephen.m3u', 'C:\Users\stephanos\MyFiles\Delphi(Lazarus)\
  4.     PlayListV24-TimeLimit-FileIsInDirectory\') then
  5.  

So I really do not know what I am doing.  Is there a way to check a file is present and simply do noting else except return a value that can be checked, with no other activity?

FileIsInDirectory() is used to resolve and compare a file name to a known path. It doesn't verify the file or directory exists. See the Docs.

You can use FileExists() with a fully-qualified file name.

Code: Pascal  [Select][+][-]
  1. sFile := 'C:\Users\stephanos\MyFiles\Delphi(Lazarus)\PlayListV24-TimeLimit-FileIsInDirectory\'PlayListStephen.m3u'';
  2. bExists := FileExists(sFiler);



Preview Lazarus 3.99 documentation at: https://dsiders.gitlab.io/lazdocsnext

stephanos

  • New Member
  • *
  • Posts: 38
Re: Finding a file, only
« Reply #2 on: July 12, 2022, 01:06:41 am »
Dear dsiders

Thank you, that solves half of the problem.

I amended the code and used FileExists()
I ran it without the target file present.
The else part of the code ran, as the file was not there.  Lovely.
I checked and the file had been created.
I ran the programme again and the file was detected.

Am I expecting too much in not wanting the programme to create a file?

Any guidance welcome

dsiders

  • Hero Member
  • *****
  • Posts: 1079
Re: Finding a file, only
« Reply #3 on: July 12, 2022, 02:42:33 am »
Dear dsiders

Thank you, that solves half of the problem.

I amended the code and used FileExists()
I ran it without the target file present.
The else part of the code ran, as the file was not there.  Lovely.
I checked and the file had been created.
I ran the programme again and the file was detected.

Am I expecting too much in not wanting the programme to create a file?

Any guidance welcome

FileExists() does not create the file. Rather than flail away at unseen code... post the code.

Preview Lazarus 3.99 documentation at: https://dsiders.gitlab.io/lazdocsnext

dseligo

  • Hero Member
  • *****
  • Posts: 1219
Re: Finding a file, only
« Reply #4 on: July 12, 2022, 08:52:11 am »
FileExists() does not create the file. Rather than flail away at unseen code... post the code.

FindFirst/FindNext doesn't create file either. So something is definitely wrong with his code or environment.

To OP: If the file is created, is it empty or does it have something in it?

stephanos

  • New Member
  • *
  • Posts: 38
Re: Finding a file, only
« Reply #5 on: July 12, 2022, 11:05:54 pm »
I have attached a file that documents the issue and on the way solved it.

Thanks to all and the suggestion to check the file content, it was a big clue

Case closed

 

TinyPortal © 2005-2018