View Full Version : How do I access drive/volume information (DiskInfo)
I suppose that the topic title is relatively self-explanatory, I was wondering if there are any examples of how I may list drives and parse them in such a way as to detect only fixed removable network cd-rom etc. Also I'd like to be able to check a drive/volume's file system; i.e. FAT[nn] NTFS, CDFS etc.
In VB/VBS I believe that the filesystemobject is used for this information but I couldn't find something similar in my quick scan over the included modules and scripts.
Thank you for any information or guidance on this 'my first question'!
ErosOlmi
24-07-2008, 18:57
Hi Jon.
The following is one possible way to get the list of drives:
'---Needed modules
uses "CONSOLE"
'---API declares needed by the script
DECLARE FUNCTION GetLogicalDriveStrings LIB "KERNEL32.DLL" ALIAS "GetLogicalDriveStringsA" (BYVAL nBufferLength AS DWORD,BYVAL lpBuffer AS DWORD) AS DWORD
'----------------------------------------------------------------------------
SUB Disk_GetList(byref DiskList() as string)
'----------------------------------------------------------------------------
'---A buffer big enough to contains the list of drives separated by a NULL char
DIM DriveString As STRING * 512
'---Get the list of drives
GetLogicalDriveStrings(len(DriveString), STRPTR(DriveString))
'---Fill the array with the found drives
SPLIT(DriveString, chr$(0) , DiskList)
END SUB
dim Disks() as string '---Define an empty array
Disk_GetList(Disks) '---Fill the array with the drive found
'---Printout results
printl "Found the following drives"
for Counter AS LONG = 1 to ubound(Disks())
printl Counter, Disks(Counter)
next
printL "---All done. Press a key when done---"
waitkey
Petr Schreiber
24-07-2008, 19:00
Here is another way,
using WMI. Retrieves installed drives and their filesystems.
uses "Console" ' -- To be able to work with console
USES "WMI" ' -- To be able to manage WMI for hardware queries
USES "OS" ' -- To be able to retrieve PC name
' -- Variables
dim driveLabels as string
dim driveName as string
dim d as long
driveLabels = GetInfoOnDrives("DeviceID", "") ' -- Return info separated by tabelators
printl driveLabels
' -- Go through all driveLabels and get their file system
for d = 1 to parsecount(driveLabels, $TAB)
' -- Retrieve just one drive label from tabelator delimited list
driveName = PARSE$(driveLabels, $TAB, d)
' -- Put out formatted text
printL "Drive " + driveName + " has " + GetInfoOnDrives("FileSystem", "DeviceID='" + driveName + "'") + " file system"
next
' -- Wait for any key before program ends
waitkey
' -- End program ( optional )
stop
' --
' -- Auxiliary function to use WMI interface, you can ignore it and simply use it :)
' --
function GetInfoOnDrives( Feature as string, Condition as string ) as string
local sBuffer as string = WMI_GetData(OS_GetComputerName, "", "", "", "Win32_LogicalDisk", Condition, Feature )
local sLine() as string
local nLines as long
local OutString as string
local i as long
' -- WMI function returns big string, following will create array which can be scaned line by line
nLines = parse(sBuffer, sLine , $CRLF)
' -- We go through all lines and retrieve needed values
for i = 1 to nLines
if instr(sLine(i), Feature) > 0 then OutString += PARSE$(sLine(i), "=", 2) + $TAB
next
OutString = rtrim$(OutString, $TAB) ' -- Remove last TAB
function = OutString
end function
Hope you will find it useful,
Petr
Thank you both, I'm especially happy that my first question attracted a positive response as opposed to a quick RTFM or use the search facility.
I was fairly certain there'd be a WMI method, I have Batch/WMIC scripts already which perform these functions, so Psch's example should help me better understand the way this language works!
In the meantime however, because I've never used a language which can call API's I'm more intrigued by the ErosOlmi response and will now spend some time playing around with my script until I have something like I intended in the first instance.
Thank you for inserting comments too, it's something I've rarely done but now I'm learning I've come to realize exactly how important they can be.
You'll no doubt be hearing from me again soon!
ErosOlmi
24-07-2008, 23:18
Jon,
I'm pretty sure you will never get a RTFM response in thinBasic forum (well, there can be a limit but very high ;) ).
Regarding thinBasic, it has full access to all Windows API. You just need to know how the API is declared and than use it like a normal function. So you have a big arsenal to play with. In many cases to simplify things, we have incorporated some API into native language functions or in some cases into complete thinBasic modules (special DLLs that add a set of new language functionalities automatically loaded at runtime). WMI is just one of those modules. We didn't develop all WMI functionalities but just the basis are able to help a lot.
Ciao
Eros
ErosOlmi
25-07-2008, 00:08
Also your request let me recall that we didn't developed much native functions about drives.
Maybe we can do something for next thinBasic release. We can extend FILE module to work also on Disks other than files and directories.
ErosOlmi
25-07-2008, 02:12
Attached a latest example in a form of a thinBasic executable. The attached example performs some new features that will be present in next thinBasic release like passing UDT elements BYREF to external DLLs (for example API). For this reason you cannot execute it with your current thinBasic so I have attached it as executable (and source code, just to haver a look).
Attention: in case you have mounted a logical network disk that is currently offline, it can takes few seconds before returning info.
Ciao
Eros
Petr Schreiber
25-07-2008, 11:46
Very cool example Eros,
only problem ( kind of GPF ) it has occurs when I try to get info from empty floppy drive ( I know it is old garbage, but I keep it for nostalgic purposes :) ).
Petr