Page 1 of 1

FileExists ?

Posted: Thu Jan 08, 2009 1:02 am
by SFSxOI
I went back to work on a small project for a utility tonight. I needed some way to see if a file already exists in a directory and thought "Gee, I wish PB had a FileExists function of some type." I searched thru the forums but got frustrated with not really finding anything, just mentions from years gone by. I seem to remember mention of a library that had it but a whole library just for a one time use is a little much, so I kludged one up real quick to test with until I find something else. It looks like this:

Code: Select all

Procedure.s FileExists(dir.s, ini.s)
  If ExamineDirectory(0, dir, ini)  
    While NextDirectoryEntry(0)
      file_exists$ = DirectoryEntryName(0)
    Wend
    FinishDirectory(0)
  EndIf
  ProcedureReturn file_exists$
EndProcedure

dir$ = "C:\test\"
ini$ = "test.ini"

If FileExists(dir$, ini$) = ""
MessageRequester("Information","File does not exist !"
Else
MessageRequester("Information","File exist !"
EndIf

Its called three times in the code and seems to work OK, but it is kinda ...well...it works anyway.

Anyone got anything better?

Posted: Thu Jan 08, 2009 1:06 am
by pdwyer

Posted: Thu Jan 08, 2009 1:15 am
by SFSxOI
Oh Crap! I can't believe I over looked that. I guess when i saw the 'size' part I ignored it. Thanks pdwyer :)

Posted: Thu Jan 08, 2009 2:12 am
by pdwyer
I made the same mistake before, my punishment was having to tell the next person who didn't notice it

Tip, you're IT ;)

Posted: Thu Jan 08, 2009 9:13 am
by Shardik
A Windows only solution would be the API function PathFileExists_() that even accepts a path in UNC notation. For other interesting solutions take a look into this similar thread:
http://www.purebasic.fr/english/viewtopic.php?t=30087

Posted: Thu Jan 08, 2009 12:20 pm
by blueznl
I also made the same mistake. Many of us did, I guess :-)

Posted: Thu Jan 08, 2009 2:00 pm
by Kaeru Gaman
thank the Gods I read it THAT often, I think I'll never forget... :lol:

Posted: Thu Jan 08, 2009 10:29 pm
by SFSxOI
Oh No !!!

The albatros around my neck is FileExists !!!

I am doomed to forever bear this burden, no one will ever ask about it again and i'll never have the chance to pass it on. Doomed forever to wander the PB forums in search of someone to tell about FileExists. Just put a big scarlett FileExists on my chest now and be done with it. :)

Thank Folks :)