PDA

View Full Version : getting list of drives installed



sandyrepope
10-11-2007, 02:06
Is there any command that can be used to get a list of the drives installed on a computer?

I went through the help file and couldn't find anything that looked like it would work.

Thanks
Sandy

Michael Clease
10-11-2007, 02:54
This will give you any hard drives fitted to your system (no CD drives)

it just checks to see if each drive letter is valid.



USES "FILE"

DIM n as BYTE
DIM path as STRING
DIM colon as STRING VALUE = ":\"
DIM DRIVES(26) AS STRING
DIM sMsg AS STRING
'
' Loop through checking if drive exists
'
FOR n = 1 to 26
path = CHR$( n + 64) + Colon
DRIVES(n) = IIF$ (DIR_Exists(Path), path, "" )
NEXT

'
' This is just for displaying results
'
sMsg += "Tested" + $TAB + "Valid" + $CRLF
FOR n = 1 to 26
sMsg += CHR$( n + 64) + Colon + $TAB ' Tested
sMsg += Drives(n) + $CRLF ' Valid
NEXT

MSGBOX 0, sMsg

sandyrepope
10-11-2007, 03:40
Abraxas, thanks for your reply. I appreciate the script. I'm hoping that I'll be able to figure out something by working with it.


This will give you any hard drives fitted to your system (no CD drives)
You are right. The script didn't find drive A, D, or E.

Thanks for the script. It gives me something to work with. I don't know why I didn't think of using dir_exists.
Sandy

Michael Clease
11-11-2007, 03:17
Here you go sandy after a bit of head scratching I found a way of doing it.



DECLARE FUNCTION GetLogicalDriveStrings LIB "KERNEL32.DLL" ALIAS "GetLogicalDriveStringsA" (BYVAL nBufferLength AS DWORD,BYVAL lpBuffer AS DWORD) AS DWORD

DIM sMsg AS STRING

SUB WhatLetters()

Dim ArrayName() As String
DIM driveString As STRING * 2000 ' this has to be setup or it doesnt work. you cannot pass an empty string. so why not make it big.
DIm lenBuffer AS DWORD VALUE len(driveString)+1
DIM NumBytes as DWORD
DIM lpBuffer as DWORD VALUE STRPTR(DriveString)
Dim Counter As DWORD
Dim nTokens As DWORD

sMsg = ""

'MSGBOX 0, " Buffer length = " + STR$(lenBuffer) + " Address = " + HEX$(lpBuffer) ' Debugging

NumBytes = GetLogicalDriveStrings(LenBuffer, lpBuffer) ' This call is to check to see if our buffer is big enough
LenBuffer = NumBytes+1
NumBytes = GetLogicalDriveStrings(LenBuffer, lpBuffer) ' This call gets the actual list.

'MSGBOX 0, "Returned " + STR$( Numbytes) + " Buffer len " + STR$(lenBuffer) ' Debugging

nTokens = SPLIT(DriveString, chr$(0) , ArrayName) ' Split the array thats returned. 0(null) is terminator.

'MSGBOX 0, "Buffer Length " + LEN(DriveString) + " ToKens " + nTokens ' Debugging
'
' This is just for displaying results
'
sMsg += "Valid Drives"+ $CRLF+$CRLF
For Counter = LBOUND(ArrayName) To UBound(ArrayName) 'nTokens
sMsg += ArrayName(Counter) & $CRLF
Next
MSGBOX 0, sMsg
' FILE_SAVE(APP_SOURCEPATH + "DATADUMP.txt" , DriveString) 'sMsg) ' Debugging
END SUB


this is from the win32api.inc and doesnt work (unless I am doing something wrong)

DECLARE FUNCTION GetLogicalDriveStrings LIB "KERNEL32.DLL" ALIAS "GetLogicalDriveStringsA" (BYVAL nBufferLength AS DWORD, lpBuffer AS ANY) AS DWORD

sandyrepope
11-11-2007, 05:04
Abraxas. I tried the script in the scriptbox and didn't get anything. I was wondering... why is everything in a sub? Also, doesn't a sub have to be called before it will be executed?

It is late at night here. I'll work more with the script tomorrow.

Thanks
Sandy

I went back and tried it again by commenting out the sub and end sub. It then worked. You can ignore the above comments.
Thank you very much for the script.
Sandy

Petr Schreiber
11-11-2007, 09:49
Hi,

very nice script!

Sandy, in thinBASIC you must not declare thinBASIC SUBs/FUNCTIONs unlike other basic dialects.
The code "did nothing" because the SUB was not called, Abraxas put it here as generall code snippet.

To make it run with SUB, just let it as it is and put following on line after "DIM sMsg AS STRING"


WhatLetters()



Bye,
Petr

Michael Clease
11-11-2007, 12:13
@Sandy. Sorry the code posted was just to show it but the attached file should have worked.

@Petr. thanks :D

@Eros. Perhaps this could be added in the "FILE" module as a new keyword when you get time.

thanks.

Mike

ErosOlmi
11-11-2007, 12:24
Sandy,

as Petr mentioned, in thinBasic a SUB or FUNCTION can be placed before or after it is used. This because thinBasic before starting to parse script for execution it parse script to search all the SUB and FUNCTIONS present in the script in order to store all the info needed to call them when encountered in execution time. So consider thinBasic a double step parsing process: first is data collection, second is script execution.

Other languages do not permit this freedom. SUB or FUNCTIONS must be encountered by the engine before their usage. Other languages introduce the concept of declaring the SUB or FUNCTION, that is, you can write your function where you prefer but if used before, you must declare function header at the beginning of the script.

Ciao
Eros

@Abraxas: good idea. I will do.
@Petr: thanks for the help.

sandyrepope
11-11-2007, 17:59
Sorry the code posted was just to show it but the attached file should have worked.
Part of my misunderstanding was caused by the fact that I downloaded the attached file but when I was trying the other script I simply forgot that I'd downloaded the attached file and didn't try it. My fault! :-[


this is from the win32api.inc

I don't know about this. What is it?

Thanks
Sandy

sandyrepope
12-11-2007, 17:33
this is from the win32api.inc

I don't know about this. What is it?
I did a search and managed to find a copy of this file. I don't know just how useful it will be but I've got it just in case I do need it.

Thanks
Sandy

Petr Schreiber
12-11-2007, 17:38
Hi Sandy,

win32api.inc Abraxas mentioned is probably header file which comes with PowerBASIC.
It contains function declaration of Win32 platform functions.


Bye,
Petr

Michael Clease
12-11-2007, 18:24
Petr is right its from powerbasic for windows API functions but I would be carefull about using it, some of the declarations seem incorrectly defined for use with thinbasic.

sandyrepope
12-11-2007, 23:55
Petr is right its from powerbasic for windows API functions but I would be carefull about using it, some of the declarations seem incorrectly defined for use with thinbasic.

What is it that makes you say that some of the declarations seem incorrectly defined for use with thinbasic? Could you explain that a little?

Thanks
Sandy

Michael Clease
13-11-2007, 00:42
Take this as an example. This one works.

DECLARE FUNCTION GetLogicalDriveStrings LIB "KERNEL32.DLL" ALIAS "GetLogicalDriveStringsA" (BYVAL nBufferLength AS DWORD,BYVAL lpBuffer AS DWORD) AS DWORD

this one is from the win32api.inc file. It doesnt work.

DECLARE FUNCTION GetLogicalDriveStrings LIB "KERNEL32.DLL" ALIAS "GetLogicalDriveStringsA" (BYVAL nBufferLength AS DWORD, lpBuffer AS ANY) AS DWORD

I did see some other functions that looked incorrect but I didnt have time to test them.

sandyrepope
13-11-2007, 02:18
That helps me to understand better. I'll be very careful when using anything from win32api.inc.

Thank you for both the explanation and the example.

Sandy