June 25, 2024, 06:05:22 PM

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


GetFileAttributes

Started by Egil, April 10, 2016, 06:33:46 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Egil


'-------------------------------------------------------------------------------
'  FileExists() - Returns INT  (  0 on success  - 1 on failure )
'-------------------------------------------------------------------------------
' Description: Checks to see if the specified file exists.
' Syntax:          FileExists(sFilePath)
' Parameter(s): sFilePath - The full filepath of the file to be checked
' Requirement: EBasic
' Return Value(s): On Success - Returns 1 as INT
' On Failure   - Returns 0 as INT
' Author(s): locodarwin at yahoo dot com
' Note(s): None.
'-------------------------------------------------------------------------------
SUB FileExists(sFilePath as STRING), INT
RETURN _GetFileAttributes(sFilePath) <> -1
ENDSUB


I have been using above code for years to check if a file already exists. But after I started to use Win7/64bit my programs often freeze when til subroutine is called. Especially if the directory i browse for the file contains many files.
Since these files mostly are temporary log files not longer needed, I just delete them, and the program using the FileExists works ok again, until the number of files are starting to grow again. The mumber of files when the  fault occours, are approximately 30.
The problem can be fixed by deleting these logfiles automaticly each time my program starts, but I have a feeling that doing it this way is just camuflaging the problem.

Does anyone know a better method to check if a file already exists?
Support Amateur Radio  -  Have a ham  for dinner!

Andy

April 10, 2016, 07:47:56 AM #1 Last Edit: April 10, 2016, 07:54:52 AM by Andy
Maybe not a better way, but you could try the

COPYFILE command.

Obviously, it won't be able to copy a file if it doesn't exist.

You could try OPENFILE if you know you can open these files?

Here is a link I started some time ago:
http://www.ionicwind.com/forums/index.php?topic=4580.0  - Maybe this can help.


Day after day, day after day, we struck nor breath nor motion, as idle as a painted ship upon a painted ocean.

LarryMc

I use the one from Fletchie's lib and don't seemed to have a problem

QuoteChecks for the existance of a file.
Syntax
FileExists(fn:string),int
Parameters
fn
[in] The file in question
Return Value
0 if does not exists, 1 if the file exists or 2 if there is a directory by that name.
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

Egil

Thanks Andy.

COPYFILE will not be ideal for me, as I'm just copying certain lines from the logfiles.
But OPENFILE will work fine in this application off course. Why didn't I thik of that myself??




Support Amateur Radio  -  Have a ham  for dinner!

Egil

Quote from: LarryMc on April 10, 2016, 07:54:41 AM
I use the one from Fletchie's lib and don't seemed to have a problem

QuoteChecks for the existance of a file.
Syntax
FileExists(fn:string),int
Parameters
fn
[in] The file in question
Return Value
0 if does not exists, 1 if the file exists or 2 if there is a directory by that name.


Hi Larry,
You had posted while I was typing...
Where can I get a copy of Fletchie's Lib?

I just wonder why the function I have used ever since I purchased EB, suddenly have started to fail. Guess the answer is that the GetFileAttributes API function does not work as it used to on 64 bits systems.
When I used the same program on XP, I deleted the logfiles once or twice a year whithout problems.

But changing the API call to OPENFILE, mentioned by Andy, will do exactly what I want.
Support Amateur Radio  -  Have a ham  for dinner!

Egil

Problem now solved.
Used OPENFILE instead of the API call. But since OPENFILE returns the opposite values of what needed for the several routines calling my FileExixts function, I had to adjust it a little. Compiled my program with the new code below, and tried in "on the air". Copied some 800 old logfiles into the directory I use for "live" work, and started the decoding plus my log extracing program. No problems observed, and the program did not freeze when trying to see if the logfiles existed. Starting the old version with the same number of logfiles, make my  program freeze.

Here is the new code:

'-------------------------------------------------------------------------------
'  FileExists() - Returns INT  (  1 on success  0 on failure )
'-------------------------------------------------------------------------------
SUB FileExists(sFilePath as STRING), INT
def lfile:file
def ret:int
if openfile(lfile,sFilePath,"R") = 0
ret = 1
closefile lfile
else
ret = 0
endif
return ret
ENDSUB


Support Amateur Radio  -  Have a ham  for dinner!

Andy

Egil,

Glad it worked for you!

Andy.
:)
Day after day, day after day, we struck nor breath nor motion, as idle as a painted ship upon a painted ocean.