Simple DriveExist procedure MULTI [Win -Mac - Linux]

Share your advanced PureBasic knowledge/code with the community.
User avatar
minimy
Enthusiast
Enthusiast
Posts: 551
Joined: Mon Jul 08, 2013 8:43 pm
Location: off world

Simple DriveExist procedure MULTI [Win -Mac - Linux]

Post by minimy »

Hi this is a simple disc exist, return true or false.
Procedure DriveExist(Drive.s)
Protected exist
If CreateFile(0,drive+"test")
exist=#True : CloseFile(0) : DeleteFile(drive+"test")
Else
exist=#False
EndIf
ProcedureReturn exist
EndProcedure


Debug DriveExist("c:\")
Debug DriveExist("J:\")
If translation=Error: reply="Sorry, Im Spanish": Endif
Little John
Addict
Addict
Posts: 4770
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Simple DriveExist procedure MULTI [Win -Mac - Linux]

Post by Little John »

If the regarding disc is write-protected, your above procedure will retuen #False, even though the disc does exist.
Here is a solution which is better IMHO.
User avatar
kenmo
Addict
Addict
Posts: 2032
Joined: Tue Dec 23, 2003 3:54 am

Re: Simple DriveExist procedure MULTI [Win -Mac - Linux]

Post by kenmo »

Code: Select all

Macro PathExists(Path)
  Bool(FileSize(Path) = -2)
EndMacro

For i = 'A' To 'Z'
  If (PathExists(Chr(i) + ":\"))
    Debug Chr(i) + ":\ exists"
  EndIf
Next i
Only tested on Windows though :)
User avatar
Tenaja
Addict
Addict
Posts: 1959
Joined: Tue Nov 09, 2010 10:15 pm

Re: Simple DriveExist procedure MULTI [Win -Mac - Linux]

Post by Tenaja »

kenmo wrote:

Code: Select all

Macro PathExists(Path)
  Bool(FileSize(Path) = -2)
EndMacro

For i = 'A' To 'Z'
  If (PathExists(Chr(i) + ":\"))
    Debug Chr(i) + ":\ exists"
  EndIf
Next i
Only tested on Windows though :)
This works nicely, except for one thing. If the drive letter is a mapped network drive, and you are not connected to the required network, you still get #True.
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: Simple DriveExist procedure MULTI [Win -Mac - Linux]

Post by PB »

> If the drive letter is a mapped network drive, and you are not
> connected to the required network, you still get #True

Same if there's no media in a DVD drive: it returns #True, even
though there's nothing there to access at all.
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
User avatar
minimy
Enthusiast
Enthusiast
Posts: 551
Joined: Mon Jul 08, 2013 8:43 pm
Location: off world

Re: Simple DriveExist procedure MULTI [Win -Mac - Linux]

Post by minimy »

PB wrote:> If the drive letter is a mapped network drive, and you are not
> connected to the required network, you still get #True

Same if there's no media in a DVD drive: it returns #True, even
though there's nothing there to access at all.
Hi PBmaniacs! ;-)..
I write a little piece of code to detect if one drive is ready to use.

Code: Select all

Procedure DiscReady(disco.s)
  Protected files.l=0,exist.b=#False
  If ExamineDirectory(0,disco+":\","*.*")
    While NextDirectoryEntry(0)
      files+1
    Wend
    FinishDirectory(0)
    If files>0:exist=#True:EndIf
  EndIf
  ProcedureReturn exist
EndProcedure

Debug DiscReady("c");  HD
Debug DiscReady("d");  CDROM
If translation=Error: reply="Sorry, Im Spanish": Endif
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

Re: Simple DriveExist procedure MULTI [Win -Mac - Linux]

Post by jassing »

minimy wrote:
PB wrote:> If the drive letter is a mapped network drive, and you are not
While your code works, if it contains lots of files (thousands) it (potentially) will cause a long delay (especially on slow media) You may want to consider something like: [aircode=on]

Code: Select all

Procedure DiscReady(disco.s)
  Protected hDir, exist.b=#False
  hDir = ExamineDirectory(#PB_ANY,disco+":\","*.*")
  If hDir 
    If NextDirectoryEntry(hDir)
      exist=#True
    EndIf
    FinishDirectory(hDir)
  EndIf
  ProcedureReturn exist
EndProcedure

Debug DiscReady("c");  HD
Debug DiscReady("d");  CDROM
Since you don't actually care if there is more than one file -- you only need to test for the one entry & be done...
User avatar
minimy
Enthusiast
Enthusiast
Posts: 551
Joined: Mon Jul 08, 2013 8:43 pm
Location: off world

Re: Simple DriveExist procedure MULTI [Win -Mac - Linux]

Post by minimy »

jassing wrote:
minimy wrote:
PB wrote:> If the drive letter is a mapped network drive, and you are not
While your code works, if it contains lots of files (thousands) it (potentially) will cause a long delay (especially on slow media) You may want to consider something like: [aircode=on]

Code: Select all

Procedure DiscReady(disco.s)
  Protected hDir, exist.b=#False
  hDir = ExamineDirectory(#PB_ANY,disco+":\","*.*")
  If hDir 
    If NextDirectoryEntry(hDir)
      exist=#True
    EndIf
    FinishDirectory(hDir)
  EndIf
  ProcedureReturn exist
EndProcedure

Debug DiscReady("c");  HD
Debug DiscReady("d");  CDROM
Since you don't actually care if there is more than one file -- you only need to test for the one entry & be done...
Thanks jassing, youre right, but i dont know another way.. the search continue friend! :wink:
If translation=Error: reply="Sorry, Im Spanish": Endif
Post Reply