Forum > General

Finding a file, only

(1/2) > >>

stephanos:
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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---var     searchResult : TSearchRec;  // Test Fileif findfirst('PlayListStephen.m3u', faAnyFile, searchResult) = 0 then // // after the ifFindClose(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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TForm1.BitBtn2Click(Sender: TObject);  // Test Filebegin     if FileIsInDirectory('PlayListStephen.m3u', 'C:\Users\stephanos\MyFiles\Delphi(Lazarus)\     PlayListV24-TimeLimit-FileIsInDirectory\') then     begin //       BitBtn3.Enabled := True;       Form1.ActiveControl := BitBtn3;       Open.Enabled := True;       Label3.Enabled := True;       Label3.Font.Color := clMaroon;       Label3.Caption := 'File present.  Click the Open File button to continue '#13#10' or                                                    press ALT + F to continue';     end     else     Begin     Label3.Caption := 'File NOT present'; //begin     end;   // End of if FileIsInDirectory('Play …….     Test.Enabled := False; BitBtn2.Enabled := False;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:

--- Quote from: stephanos on July 11, 2022, 02:37:59 pm ---
--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---if findfirst('PlayListStephen.m3u', faAnyFile, searchResult) = 0 then // 

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TForm1.BitBtn2Click(Sender: TObject);  // Test Filebegin     if FileIsInDirectory('PlayListStephen.m3u', 'C:\Users\stephanos\MyFiles\Delphi(Lazarus)\     PlayListV24-TimeLimit-FileIsInDirectory\') then  
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?

--- End quote ---

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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---sFile := 'C:\Users\stephanos\MyFiles\Delphi(Lazarus)\PlayListV24-TimeLimit-FileIsInDirectory\'PlayListStephen.m3u'';bExists := FileExists(sFiler);


stephanos:
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:

--- Quote from: stephanos 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

--- End quote ---

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

dseligo:

--- Quote from: dsiders on July 12, 2022, 02:42:33 am ---FileExists() does not create the file. Rather than flail away at unseen code... post the code.

--- End quote ---

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?

Navigation

[0] Message Index

[#] Next page

Go to full version