Searching for Drives

Windows specific forum
RichardL
Enthusiast
Enthusiast
Posts: 532
Joined: Sat Sep 11, 2004 11:54 am
Location: UK

Searching for Drives

Post by RichardL »

There are a large number of card readers that allow Compact Flash, SD, MMC etc cards to appear as standard disk drives. For example, my DANE-ELEC USB2 plugs into the USB port and offers four drives, F:,G:,H: and I:

The program I am involved with needs to scan all possible drives from C: to N: and then check to see if a specific file (DATA_LOG.BIN) exists on it.

Using GFA Basic I managed to check for drives by attempting to open the file <Drive$+":\NUL"> and using TRY ... CATCH to detect errors due to non existant drives. Then I used IF EXIST(drive$+"\DATA.BIN") to confirm the file existed. This worked Ok and no system messages ever appeared.

Using Pure BASIC, has anyone managed to perform a scan for drives without any system error messages appearing?

Having found the drive I intend checking for the file (always in root) by examining the return from ReadFile()
Pupil
Enthusiast
Enthusiast
Posts: 715
Joined: Fri Apr 25, 2003 3:56 pm

Post by Pupil »

Try this to get available drives (uses WIN API so it's not crossplatform):

Code: Select all

Dim DriveString.s(30)

*buffer = AllocateMemory(256)
If *buffer = 0
  ; unable to allocate memory
  End
EndIf

If GetLogicalDriveStrings_(254, *buffer)
  pos.l = *buffer
  index = 0
  Repeat
    DriveString(index) = PeekS(pos)
    If DriveString(index) = ""
      Break
    Else
      pos + Len(DriveString(index)) + 1
    EndIf
    index+1
  ForEver
  
  For i = 0 To index-1
    Debug DriveString(i)
  Next
Else
  ; Call failed, need some error handling
  End
EndIf

FreeMemory(*buffer)
Also for checking if a file exist, see documention on the 'FileSize()' command.
RichardL
Enthusiast
Enthusiast
Posts: 532
Joined: Sat Sep 11, 2004 11:54 am
Location: UK

Post by RichardL »

Thanks for the suggestion. I added a file size check using FileSize(), but I get a "No disk in the drive......" message requester from the system for the floppy drive and the empty card slots in the card reader.

Code: Select all

Dim DriveString.s(30)

*buffer = AllocateMemory(256)
If *buffer = 0
  ; unable to allocate memory
  End
EndIf

If GetLogicalDriveStrings_(254, *buffer)
  pos.l = *buffer
  Index = 0
  Repeat
    DriveString(Index) = PeekS(pos)
    If DriveString(Index) = ""
      Break
    Else
      pos + Len(DriveString(Index)) + 1
    EndIf
    Index+1
  ForEver

  For i = 0 To Index-1
    If FileSize(DriveString(i)+"DATA_LOG.BIN")<>-1
      Debug "Found on drive:"+DriveString(i)
    Else
      Debug "Not on drive  : "+DriveString(i)
    EndIf    
  Next
  
Else
  ; Call failed, need some error handling
  End
EndIf
What is needed is a test that the drive has a valid media... followed by a test for the media having the file.
RichardL
Enthusiast
Enthusiast
Posts: 532
Joined: Sat Sep 11, 2004 11:54 am
Location: UK

Post by RichardL »

I've done some further digging and it looks like I should be able to take control of error messages by using the API's SetErrorMode(), but PB does not recognise it.
GreenGiant
Enthusiast
Enthusiast
Posts: 252
Joined: Fri Feb 20, 2004 5:43 pm

Post by GreenGiant »

Recognises it for me.

Code: Select all

SetErrorMode_(uMode)
RichardL
Enthusiast
Enthusiast
Posts: 532
Joined: Sat Sep 11, 2004 11:54 am
Location: UK

Post by RichardL »

I'm using jaPBe. :D .. which does not recognise it.
The standard PB editor :cry: does not.
Anyone care to suggest a reason?
thefool
Always Here
Always Here
Posts: 5875
Joined: Sat Aug 30, 2003 5:58 pm
Location: Denmark

Post by thefool »

command should be supported, the editer just dont show it. Try it, it will work.
GreenGiant
Enthusiast
Enthusiast
Posts: 252
Joined: Fri Feb 20, 2004 5:43 pm

Post by GreenGiant »

But I'm using the standard editor and it recognises it for me. When I type the command it automatically capitalises (not sure thats the word :P ) it for me, and shows the parameters in the status bar at the bottom.
thefool
Always Here
Always Here
Posts: 5875
Joined: Sat Aug 30, 2003 5:58 pm
Location: Denmark

Post by thefool »

ah ok. i just checked with japbe. it did compile but no parameters list..
But the standard editor does. Sorry :oops:
RichardL
Enthusiast
Enthusiast
Posts: 532
Joined: Sat Sep 11, 2004 11:54 am
Location: UK

Post by RichardL »

OK, I getting there! The following code works... I'm not sure that it is good code, but until tomorrow I'm happy to have something that works.

No system messages popping up despite several drives being empty... and the floppy rattles.

Ten hours at the keyboard! I'm off to open a bottle (maybe two) and cook a nice piece of salmon.

Code: Select all

Dim DriveString.s(30)

*buffer = AllocateMemory(256)
If *buffer = 0
  ; unable to allocate memory
  End
EndIf

If GetLogicalDriveStrings_(254, *buffer)
  pos.l = *buffer
  Index = 0
  Repeat
    DriveString(Index) = PeekS(pos)
    If DriveString(Index) = ""
      Break
    Else
      pos + Len(DriveString(Index)) + 1
    EndIf
    Index+1
  ForEver
  
  k2$="Not found"
  ;SEM_FAILCRITICALERRORS = 00001h
  OldErrorMode.l = SetErrorMode_(1)
  For i = 0 To Index-1
    k$=DriveString(i)+"DATA_LOG.BIN"
    If GetFileAttributes_(@k$)=-1
      Junk.l=GetLastError_()
      SetLastError_(0)     
    Else
      k2$="Gotit"
    EndIf
  Next i
  SetErrorMode_(OldErrorMode.l)
  Debug k2$
Else
  ; Call failed, need some error handling
  End
EndIf
Good night all... :D
Post Reply