FileExists ?

Everything else that doesn't fall into one of the other PB categories.
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

FileExists ?

Post 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?
User avatar
pdwyer
Addict
Addict
Posts: 2813
Joined: Tue May 08, 2007 1:27 pm
Location: Chiba, Japan

Post by pdwyer »

Paul Dwyer

“In nature, it’s not the strongest nor the most intelligent who survives. It’s the most adaptable to change” - Charles Darwin
“If you can't explain it to a six-year old you really don't understand it yourself.” - Albert Einstein
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Post 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 :)
User avatar
pdwyer
Addict
Addict
Posts: 2813
Joined: Tue May 08, 2007 1:27 pm
Location: Chiba, Japan

Post 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 ;)
Paul Dwyer

“In nature, it’s not the strongest nor the most intelligent who survives. It’s the most adaptable to change” - Charles Darwin
“If you can't explain it to a six-year old you really don't understand it yourself.” - Albert Einstein
User avatar
Shardik
Addict
Addict
Posts: 2075
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Post 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
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6172
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

I also made the same mistake. Many of us did, I guess :-)
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB - upgrade incoming...)
( The path to enlightenment and the PureBasic Survival Guide right here... )
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

thank the Gods I read it THAT often, I think I'll never forget... :lol:
oh... and have a nice day.
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Post 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 :)
Post Reply