July 02, 2024, 07:49:37 AM

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


Drives attached to the system

Started by ckoehn, January 28, 2010, 08:41:54 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

ckoehn

Is there a way to find out the available drives on your system?  Such as flash drives, SD cards, ect.  To try and open a drive that is not there generates an error.  How can this be prevented?

BumbleBee

Hi,

Try this:


DECLARE "kernel32",GetLogicalDriveStringsA(length:INT,pStr:STRING),INT

DEF temp$ :STRING
GetLogicalDriveStringsA(255,temp$)


You will get the drive letters separated with null strings in temp$.

Bye,
B.

ckoehn

I tried the GetLogicalDrives() which returned a bitmask for all the currently available drives.  It returned all the letters from A-Z.  The code that I used is one that is with the API Guide 3.7 (Copyright ÂÃ,© The KPD-Team, 1998-2002).  Maybe I did missed something.

DEF LDs,Cnt:INT
DEF sDrives:STRING

LDs = GetLogicalDrives()
sDrives = ""
For Cnt = 0 To 25
If (LDs And 2 ^ Cnt) <> 0 Then
sDrives = sDrives + Chr$(65 + Cnt)
End If
Next Cnt

billhsln

This is some code I had laying around.  I did not write it, so who ever did, Thank you....

$MAIN
AutoDefine "Off"
Def Version$:String
Version$="1.0"

declare "kernel32",GetDriveTypeFL alias GetDriveTypeA(dir:string),int
declare "kernel32",GetLogicalDrivesFL alias GetLogicalDrives(),int
declare "kernel32",GetFileAttributesAFL alias GetFileAttributesA(fn:string),int

declare GetDriveTypeStrFL(dir:string),string
declare GetAllDrivesStr(),string
declare ValidFileNameFL(fn:string),int
declare FileExtensionFL(filename:string),string
declare FileExistsFL(filename:string),int
declare DirExistsFL(dir:string),int
declare GetFileAttrFL(fn:string),string
declare ConvFileAttrFL(a:int),string

setid "FILE_ATTRIBUTE_ARCHIVEFL",0x20
setid "FILE_ATTRIBUTE_COMPRESSEDFL",0x800
setid "FILE_ATTRIBUTE_DIRECTORYFL",0x10
setid "FILE_ATTRIBUTE_HIDDENFL",0x2
setid "FILE_ATTRIBUTE_NORMALFL",0x80
setid "FILE_ATTRIBUTE_READONLYFL",0x1
setid "FILE_ATTRIBUTE_SYSTEMFL",0x4
setid "FILE_ATTRIBUTE_TEMPORARYFL",0x100

def Drives, rtn, dir:string
def i, l:int

openconsole
Drives = GetAllDrivesStr()
print Drives
l = len(Drives)
for i = 1 to l
dir = mid$(Drives,i,1)
print dir, " ", GetDriveTypeStrFL(dir + ":\\")
next i
print " "
input "Press RETURN",rtn
closeconsole
end

' GetDriveTypeStrFL
sub GetDriveTypeStrFL(dir:string),string
def t:string
def n:int
t=dir
if (right$(dir,1)<>"\\") then t=t+"\\"
n = GetDriveTypeFL(t)
select n
case 2
return "Removable"
case 3
return "Fixed"
case 4
return "Remote"
case 5
return "CD-ROM"
case 6
return "RamDisk"
default
return str$(n)
endselect
endsub

' GetAllDrivesStr
sub GetAllDrivesStr(),string
def n, t, s:int
def d:string
d = ""
n = GetLogicalDrivesFL()
for t=0 to 25
s = 2 ^ t
if (n & s)
d += chr$(65 + t)
endif
next t
return d
EndSub

' ValidFileNameFL
sub ValidFileNameFL(fn:string),int
def s,t:int
if len(fn)>0
s=1
for t=1 to len(fn)
if instr("\\/:*?<>|"+"\"",mid$(fn,t,1))>0
s=0
endif
next t
endif
return s
EndSub

' FilenpathFL
sub FilenpathFL(full:string,filename:pointer,path:pointer)
def n:int
def fn:string
fn=ltrim$(rtrim$(full))
if instr(fn,"\\")>0
n=len(fn)
while mid$(fn,n,1)<>"\\"
n=n-1
endwhile
#<string>path=left$(fn,n)
#<string>filename=mid$(fn,n+1)
else
#<string>path=""
#<string>filename=fn
endif
return
EndSub

' FileExtensionFL
sub FileExtensionFL(filename:string),string
def n:int
def fn,stfn,path:string
fn=ltrim$(rtrim$(filename))
FilenpathFL(fn,stfn,path)
if instr(stfn,".")>0
n=len(stfn)
while mid$(stfn,n,1)<>"."
n=n-1
endwhile
return lcase$(mid$(stfn,n))
else
return ""
endif
endsub

' FileExistsFL
sub FileExistsFL(filename:string),int
def fv:file
if (openfile(fv,filename,"r")=0)
closefile fv
return 1
else
return 0
endif
endsub

' DirExistsFL
sub DirExistsFL(dir:string),int
def a:int
def t:string
t=ltrim$(rtrim$(dir))
if right$(t,1)<>"\\" then t=t+"\\"
a=findopen(t+".")
if a=0
return 0
else
findclose a
return 1
endif
endsub

' GetFileAttrFL
sub GetFileAttrFL(fn:string),string
return ConvFileAttrFL(GetFileAttributesAFL(fn))
EndSub

' ConvFileAttrFL
sub ConvFileAttrFL(a:int),string
def r:string
if a<>-1
if a & @FILE_ATTRIBUTE_ARCHIVEFL then r=r+"A"
if a & @FILE_ATTRIBUTE_COMPRESSEDFL then r=r+"C"
if a & @FILE_ATTRIBUTE_DIRECTORYFL then r=r+"D"
if a & @FILE_ATTRIBUTE_HIDDENFL then r=r+"H"
if a & @FILE_ATTRIBUTE_NORMALFL then r=r+"N"
if a & @FILE_ATTRIBUTE_READONLYFL then r=r+"R"
if a & @FILE_ATTRIBUTE_SYSTEMFL then r=r+"S"
if a & @FILE_ATTRIBUTE_TEMPORARYFL then r=r+"T"
endif
return r
EndSub


I hope some of this is useful to you.

Bill
When all else fails, get a bigger hammer.

ckoehn

Thanks Bill for that piece of code.  It does return the available drives on the system.  I needed that.

I also need to have a way to see if a disk is in a drive or not.  That is the error that I am encountering now.

I am wanting to run a program that waits unit the user places a disk in the drive that has a specific file on it.  If it can't find the file, it waits for a few seconds and checks the drives again.  Anyone have any ideas?