simple scan for drive letters

Post your FreeBASIC source, examples, tips and tricks here. Please don’t post code without including an explanation.
Post Reply
thrive4
Posts: 70
Joined: Jun 25, 2021 15:32

simple scan for drive letters

Post by thrive4 »

As ever there are many ways to Rome...
This code uses / misuses chdir to poll
for a drive while looping through
the alphabet A to Z.
Only for an OS that utilizes drive letters.

On a side note for nix systems a possible
solution would be to shell and use one of these
methods output the result to a text file and
then parse with freebasic.
https://linoxide.com/list-mounted-drives-on-linux/

edit 28/10/2021
Tweaked the code a bit using command(0) to set
the apppath was not such a hot idea.

As some reply's indicate there are many ways to Rome ;)

A shell approach for windows could be:
> fsutil fsinfo drives
and parse the output with freebasic
More info:
http://www.cryer.co.uk/brian/windows/ba ... etters.htm

Oh and one more thing it seems that chdir
does not recognize CD drives or the virtual kind
unless it contains a disc or is mounted with an iso
so that might be a thing to keep in mind.

Code: Select all

' scan for drive letters
' 2021 by thrive4

dim apppath as string = curdir

' setup drive scan
dim i as integer = 1
dim drivescan as integer = 65
dim drivenum  as integer = 1
dim driveletter(1 to 26) as string
dim dummy as string

' scan for driveletters ascii 65 to 90 aka A to Z
do while drivescan < 91
    dummy = chr(drivescan)
    IF chdir(dummy + ":\") = 0 then
        driveletter(drivenum) = dummy
        drivenum += 1
    end if
    drivescan += 1
loop

' show driveletters found
do while i < drivenum
        print "found " + driveletter(i) + ":\"
        i += 1
loop
i = 1
drivenum = 1

' restore to application path
chdir(apppath)

sleep 5000
end
Last edited by thrive4 on Oct 28, 2021 11:33, edited 2 times in total.
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: simple scan for drive letters

Post by jj2007 »

Have you tried GetLogicalDriveStrings?
deltarho[1859]
Posts: 4292
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: simple scan for drive letters

Post by deltarho[1859] »

Here is a Windows version which uses wmic. The console window opens briefly, is parsed and output to a message box. Besides drive letters, it also shows the disk free space.

Added: I forgot to mention — compile for gui.

Code: Select all

#Include once "windows.bi" ' Needed to use Messagebox
Dim As Wstring * 32768 sBuffer
Dim As string NewBuffer
Dim As Uinteger x, BufferEnd
Dim As Double FreeSpace, TotalSpace, PerCent

Shell( "wmic logicaldisk get size,freespace,caption > DS.txt" )
Open "DS.txt" For Binary As #1
  Get #1, , sBuffer
  x = Instr( sBuffer, Chr(13,10) ) + 2
  Do
    NewBuffer += "  " + Mid( sBuffer, x, 2)
    FreeSpace = Val( Mid(sBuffer, x + 2) )
    TotalSpace = Val( Mid(sBuffer, x + 23) )
    PerCent = 100*FreeSpace/TotalSpace
    If FreeSpace < 1073741824 Then
      FreeSpace /= (1024*1024)
      NewBuffer += "  " + Str( Int(FreeSpace) ) + " MiB" + " " + Iif(FreeSpace = 0, " ", " " + _
      Str(Int(PerCent)) + "%") + chr(13,10)
    else
      FreeSpace /= (1024*1024*1024)
      NewBuffer += "  " + Str( Int(FreeSpace) ) + " GiB" + " " + Iif(FreeSpace = 0, " ", " " + _
      Str(Int(PerCent)) + "%") + Chr(13,10)
    End If
    BufferEnd = Instr( x, sBuffer, Chr(13,10) )
    x = BufferEnd + 2
  Loop Until  Instr( x, sBuffer, Chr(13,10) ) = 0
Close #1
Kill "DS.txt"
Messagebox( Null, NewBuffer, "Disk free space", Mb_Ok )
thrive4
Posts: 70
Joined: Jun 25, 2021 15:32

Re: simple scan for drive letters

Post by thrive4 »

jj2007 wrote:Have you tried GetLogicalDriveStrings?
Not really but thanks for the tip:
https://docs.microsoft.com/en-us/window ... icaldrives
thrive4
Posts: 70
Joined: Jun 25, 2021 15:32

Re: simple scan for drive letters

Post by thrive4 »

deltarho[1859] wrote:Here is a Windows version which uses wmic. The console window opens briefly, is parsed and output to a message box. Besides drive letters, it also shows the disk free space.
He, now this is a nice one, muchos gracias for the code.
I just edited my inital post and suggested a shell
approach '> fsutil fsinfo drives' how ever the wmic
is quite sexy to!
Post Reply