How to know if a disk unit is connected?.

Just starting out? Need help? Post your questions and find answers here.
User avatar
graves
Enthusiast
Enthusiast
Posts: 160
Joined: Wed Oct 03, 2007 2:38 pm
Location: To the deal with a pepper

How to know if a disk unit is connected?.

Post by graves »

Hi,
I've searched on the forum about this question, and no results.
I need to connect a Virtual disk in a unit selected by user, but previously I need to know if this unit is connected.
My approach is:

Code: Select all

 Units.s  = "DEFGHIJKLMNOPQRSTUVWXYZ"
   for n=1 to len(units)   
      chku.s = mid(units,n,1) + ":\"
      if ExamineDirectory(0, chku, "*.*")
; unit is connected 
        FinishDirectory(0)
      else
; unit is NOT connected
It's possible to check without Examinedirectory(), only with a test like "if FileSize" or so?
I've tried it with Filesize(mid(units,n,1) + ":\.") and it seems not run.

Regards
User avatar
minimy
Enthusiast
Enthusiast
Posts: 349
Joined: Mon Jul 08, 2013 8:43 pm

Re: How to know if a disk unit is connected?.

Post by minimy »

Hi, if only want use PB comands, this can be a simple solution.
I think is multiplataform.

Code: Select all

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
Deluxe0321
User
User
Posts: 69
Joined: Tue Sep 16, 2008 6:11 am
Location: ger

Re: How to know if a disk unit is connected?.

Post by Deluxe0321 »

FileSize("<drive>") should do the job.

Windows:
Will return -2 if the drive is connected
Will return -1 if not

Linux / MacOS:
Will return 0 if the drive is connected
Will return -1 if not.

Not the best solution, but should work.

Code: Select all

Procedure isDriveConnected(Path.s)
  
  If Path.s = ""
    ProcedureReturn #False    
  EndIf
  
  Protected Status.i = FileSize(Path.s)
  
  If Not (Status.i = 0 Or Status.i = -2)
    ProcedureReturn #False  
  EndIf
  
  ProcedureReturn #True
  
EndProcedure


CompilerSelect #PB_Compiler_OS
    CompilerCase #PB_OS_Windows
      Debug isDriveConnected("C:\")
      Debug isDriveConnected("H:\")
    CompilerDefault 
      OpenConsole()
      PrintN("--> "+ isDriveConnected("/dev/disk/by-uuid/25da5e6e-6cc6-46d7-894f-82f2c54da975"))
      PrintN("--> "+ isDriveConnected("/dev/disk/by-uuid/DEADBEEF_IM_NOT_EXISTING"))
CompilerEndSelect
Post Reply