Page 1 of 1
Searching for Drives
Posted: Sat Sep 18, 2004 12:39 pm
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()
Posted: Sat Sep 18, 2004 1:17 pm
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.
Posted: Sat Sep 18, 2004 4:48 pm
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.
Posted: Sat Sep 18, 2004 5:50 pm
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.
Posted: Sat Sep 18, 2004 6:17 pm
by GreenGiant
Posted: Sat Sep 18, 2004 7:05 pm
by RichardL
I'm using jaPBe.

.. which does not recognise it.
The standard PB editor

does not.
Anyone care to suggest a reason?
Posted: Sat Sep 18, 2004 7:20 pm
by thefool
command should be supported, the editer just dont show it. Try it, it will work.
Posted: Sat Sep 18, 2004 7:35 pm
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

) it for me, and shows the parameters in the status bar at the bottom.
Posted: Sat Sep 18, 2004 7:41 pm
by thefool
ah ok. i just checked with japbe. it did compile but no parameters list..
But the standard editor does. Sorry

Posted: Sat Sep 18, 2004 8:13 pm
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...
