Page 1 of 2

List Drives with Free Space

Posted: Wed Oct 02, 2019 2:35 pm
by collectordave
I am being a little dumb here I think.

Searched forum. Just need to list drives on computer with if possible the free space.

Tried using Explorer list gadget but no luck.

Cross platform if at all possible.

What am I missing?

regards

CD

Re: List Drives with Free Space

Posted: Wed Oct 02, 2019 3:02 pm
by NicTheQuick
At first there is big difference between Windows, Linux and Mac. On Linux you probably have mounted a lot of filesystems. Some of them are real drives, but most of them are temporary filesystems or devices systems. It seems you want to list the block devices like mounted ISO, mounted SSDs or HDDs, maybe mounted network file systems or webdav mounts.

You can find information about all of these mount points either in /proc/mounts or /proc/self/mountinfo. I don't know if there is direct API to get these information.

As example this is my /proc/mounts:

Code: Select all

sysfs        /sys                                  sysfs            rw,nosuid,nodev,noexec,relatime                                                 0  0
proc         /proc                                 proc             rw,nosuid,nodev,noexec,relatime                                                 0  0
udev         /dev                                  devtmpfs         rw,nosuid,relatime,size=7967276k,nr_inodes=1991819,mode=755                     0  0
devpts       /dev/pts                              devpts           rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000                           0  0
tmpfs        /run                                  tmpfs            rw,nosuid,noexec,relatime,size=1598532k,mode=755                                0  0
/dev/sda1    /                                     ext4             rw,noatime,errors=remount-ro                                                    0  0
securityfs   /sys/kernel/security                  securityfs       rw,nosuid,nodev,noexec,relatime                                                 0  0
tmpfs        /dev/shm                              tmpfs            rw,nosuid,nodev                                                                 0  0
tmpfs        /run/lock                             tmpfs            rw,nosuid,nodev,noexec,relatime,size=5120k                                      0  0
tmpfs        /sys/fs/cgroup                        tmpfs            ro,nosuid,nodev,noexec,mode=755                                                 0  0
cgroup2      /sys/fs/cgroup/unified                cgroup2          rw,nosuid,nodev,noexec,relatime,nsdelegate                                      0  0
cgroup       /sys/fs/cgroup/systemd                cgroup           rw,nosuid,nodev,noexec,relatime,xattr,name=systemd                              0  0
pstore       /sys/fs/pstore                        pstore           rw,nosuid,nodev,noexec,relatime                                                 0  0
bpf          /sys/fs/bpf                           bpf              rw,nosuid,nodev,noexec,relatime,mode=700                                        0  0
cgroup       /sys/fs/cgroup/memory                 cgroup           rw,nosuid,nodev,noexec,relatime,memory                                          0  0
cgroup       /sys/fs/cgroup/freezer                cgroup           rw,nosuid,nodev,noexec,relatime,freezer                                         0  0
cgroup       /sys/fs/cgroup/perf_event             cgroup           rw,nosuid,nodev,noexec,relatime,perf_event                                      0  0
cgroup       /sys/fs/cgroup/pids                   cgroup           rw,nosuid,nodev,noexec,relatime,pids                                            0  0
cgroup       /sys/fs/cgroup/cpu,cpuacct            cgroup           rw,nosuid,nodev,noexec,relatime,cpu,cpuacct                                     0  0
cgroup       /sys/fs/cgroup/blkio                  cgroup           rw,nosuid,nodev,noexec,relatime,blkio                                           0  0
cgroup       /sys/fs/cgroup/rdma                   cgroup           rw,nosuid,nodev,noexec,relatime,rdma                                            0  0
cgroup       /sys/fs/cgroup/net_cls,net_prio       cgroup           rw,nosuid,nodev,noexec,relatime,net_cls,net_prio                                0  0
cgroup       /sys/fs/cgroup/cpuset                 cgroup           rw,nosuid,nodev,noexec,relatime,cpuset                                          0  0
cgroup       /sys/fs/cgroup/hugetlb                cgroup           rw,nosuid,nodev,noexec,relatime,hugetlb                                         0  0
cgroup       /sys/fs/cgroup/devices                cgroup           rw,nosuid,nodev,noexec,relatime,devices                                         0  0
systemd-1    /proc/sys/fs/binfmt_misc              autofs           rw,relatime,fd=34,pgrp=1,timeout=0,minproto=5,maxproto=5,direct,pipe_ino=13169  0  0
mqueue       /dev/mqueue                           mqueue           rw,relatime                                                                     0  0
hugetlbfs    /dev/hugepages                        hugetlbfs        rw,relatime,pagesize=2M                                                         0  0
debugfs      /sys/kernel/debug                     debugfs          rw,relatime                                                                     0  0
fusectl      /sys/fs/fuse/connections              fusectl          rw,relatime                                                                     0  0
configfs     /sys/kernel/config                    configfs         rw,relatime                                                                     0  0
tmpfs        /tmp                                  tmpfs            rw,noatime                                                                      0  0
/dev/loop1   /snap/pycharm-community/155           squashfs         ro,nodev,relatime                                                               0  0
/dev/loop2   /snap/code/14                         squashfs         ro,nodev,relatime                                                               0  0
/dev/loop4   /snap/gtk-common-themes/1313          squashfs         ro,nodev,relatime                                                               0  0
/dev/loop0   /snap/gnome-3-28-1804/67              squashfs         ro,nodev,relatime                                                               0  0
/dev/loop3   /snap/gping/13                        squashfs         ro,nodev,relatime                                                               0  0
/dev/loop5   /snap/gnome-3-28-1804/71              squashfs         ro,nodev,relatime                                                               0  0
/dev/loop6   /snap/core/7396                       squashfs         ro,nodev,relatime                                                               0  0
/dev/loop8   /snap/core18/1098                     squashfs         ro,nodev,relatime                                                               0  0
/dev/loop10  /snap/code/15                         squashfs         ro,nodev,relatime                                                               0  0
/dev/loop9   /snap/core/7713                       squashfs         ro,nodev,relatime                                                               0  0
/dev/loop11  /snap/gnome-characters/317            squashfs         ro,nodev,relatime                                                               0  0
/dev/loop12  /snap/gnome-3-26-1604/90              squashfs         ro,nodev,relatime                                                               0  0
/dev/loop13  /snap/core18/1144                     squashfs         ro,nodev,relatime                                                               0  0
/dev/loop15  /snap/gnome-characters/296            squashfs         ro,nodev,relatime                                                               0  0
/dev/loop16  /snap/gtk-common-themes/1198          squashfs         ro,nodev,relatime                                                               0  0
/dev/loop14  /snap/pycharm-community/150           squashfs         ro,nodev,relatime                                                               0  0
/dev/loop17  /snap/gnome-logs/73                   squashfs         ro,nodev,relatime                                                               0  0
/dev/loop18  /snap/gnome-3-26-1604/92              squashfs         ro,nodev,relatime                                                               0  0
/dev/sda3    /media/nicolas/FastData               ext4             rw,noatime                                                                      0  0
/dev/sdb1    /home                                 ext4             rw,noatime                                                                      0  0
/dev/sdb1    /home/nicolas/ownCloud/workspace_avr  ext4             rw,nosuid,nodev,noexec,noatime                                                  0  0
/dev/sdb1    /home/nicolas/ownCloud/workspace_arm  ext4             rw,nosuid,nodev,noexec,noatime                                                  0  0
binfmt_misc  /proc/sys/fs/binfmt_misc              binfmt_misc      rw,relatime                                                                     0  0
tmpfs        /run/user/1000                        tmpfs            rw,nosuid,nodev,relatime,size=1598528k,mode=700,uid=1000,gid=1000               0  0
gvfsd-fuse   /run/user/1000/gvfs                   fuse.gvfsd-fuse  rw,nosuid,nodev,relatime,user_id=1000,group_id=1000                             0  0
/dev/loop19  /snap/gnome-logs/81                   squashfs         ro,nodev,relatime                                                               0  0

Re: List Drives with Free Space

Posted: Wed Oct 02, 2019 3:13 pm
by collectordave
Just need to list physical drives for user to choose.

It is for a backup system so can be limited to usb drives etc.

Re: List Drives with Free Space

Posted: Wed Oct 02, 2019 3:40 pm
by Marc56us
Based on:
http://forums.purebasic.com/english/vie ... 13&t=26763

Code: Select all

For a = 65 To 90 : a$=Chr(a) + ":\"
    Select GetDriveType_(a$)
        Case #DRIVE_FIXED 
            Debug a$ + " HDD/SSD"
        Case #DRIVE_REMOVABLE 
            Debug a$ + " USB"
    EndSelect
Next

Code: Select all

C:\ HDD/SSD
D:\ HDD/SSD
E:\ USB
Ref. https://msdn.microsoft.com/fr-fr/window ... p/aa364939

Re: List Drives with Free Space

Posted: Wed Oct 02, 2019 3:40 pm
by collectordave
Having some success on MAC with

Code: Select all

  InitialPath$ = "/Volumes/"   ; set initial path to display (could also be blank)
  
  Path$ = PathRequester("Please choose your path", InitialPath$)
  If Path$
    MessageRequester("Information", "You have selected following path:"+Chr(10)+Path$, 0)
  Else
    MessageRequester("Information", "The requester was canceled.", 0) 
  EndIf
Lists the drives and lets me pick one.

Anything similar in Windows?

Re: List Drives with Free Space

Posted: Thu Oct 03, 2019 7:48 am
by collectordave
A little more on the MAC.

Code: Select all

Global TopDriveFolder.s,SelectedDrive.s

TopDriveFolder = "/Volumes/"

If OpenWindow(0, 0, 0, 400, 200, "ExplorerListGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    ExplorerListGadget(0, 10, 10, 380, 180, TopDriveFolder, #PB_Explorer_NoDirectoryChange|#PB_Explorer_NoParentFolder)
    Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
    
    SelectedDrive = TopDriveFolder + GetGadgetItemText(0,GetGadgetState(0))
    
    If CreateDirectory(SelectedDrive + "/Test/") = 0
      
      MessageRequester("Unable To Create Backup Folder!", "Please Choose Another Drive.",#PB_MessageRequester_Ok)
    EndIf
    
EndIf
Lists my drives and allows me to pick one then create a folder for backup. Rejects main drive as it needs permission.

Just do this on windows now.

Re: List Drives with Free Space

Posted: Thu Oct 03, 2019 9:09 am
by BarryG
collectordave wrote:need to list drives on computer with if possible the free space
For Windows:

Code: Select all

For d='A' To 'Z'
  d$=Chr(d)+":\"
  t=GetDriveType_(d$)
  If t=#DRIVE_REMOVABLE Or t=#DRIVE_FIXED
    If GetDiskFreeSpaceEx_(@d$,@free.q,@total.q,0)
      Debug d$+" free bytes: "+Str(free)
    EndIf
  EndIf
Next

Re: List Drives with Free Space

Posted: Thu Oct 03, 2019 1:05 pm
by Sicro
I just created an include to determine the mounted drives: GetDrivesInfo() (Currently only Linux and Mac)

Code: Select all

Structure DrivesListStruc
  fsType$ ; devtmpfs, tmpfs, ext4, vfat
  avail$  ; 1,6G, 300M
  size$   ; 1,6G, 300M
  target$ ; /, /tmp, /boot/efi, /run/media/username/medianame
EndStructure

NewList drivesList.DrivesListStruc()
GetDrivesInfo(drivesList())
Besides:

Re: List Drives with Free Space

Posted: Thu Oct 03, 2019 1:16 pm
by infratec
Without API stuff:

Code: Select all

For Drive = 'A' To 'Z'
  Dir = ExamineDirectory(#PB_Any, Chr(Drive) + ":/", "*")
  If Dir
    Debug Chr(Drive)
    FinishDirectory(Dir)
  EndIf
Next Drive

Re: List Drives with Free Space

Posted: Thu Oct 03, 2019 1:18 pm
by BarryG
Nice, infratec. :)

Re: List Drives with Free Space

Posted: Thu Oct 03, 2019 2:25 pm
by wilbert
collectordave wrote:Having some success on MAC with
Did you see this post by Shardik ?
viewtopic.php?p=415435#p415435

Re: List Drives with Free Space

Posted: Thu Oct 03, 2019 3:13 pm
by collectordave
No

Thanks for pointing me in the right direction.

CD

Re: List Drives with Free Space

Posted: Mon Apr 07, 2025 9:19 am
by Randy Walker
BarryG wrote: Thu Oct 03, 2019 9:09 am
collectordave wrote:need to list drives on computer with if possible the free space
For Windows:

Code: Select all

For d='A' To 'Z'
  d$=Chr(d)+":\"
  t=GetDriveType_(d$)
  If t=#DRIVE_REMOVABLE Or t=#DRIVE_FIXED
    If GetDiskFreeSpaceEx_(@d$,@free.q,@total.q,0)
      Debug d$+" free bytes: "+Str(free)
    EndIf
  EndIf
Next
Very Nice! Thank you BarryG!!!

Re: List Drives with Free Space

Posted: Mon Apr 07, 2025 1:26 pm
by BarryG
I forgot about that post. :) Glad I could help.

Re: List Drives with Free Space

Posted: Mon Apr 07, 2025 2:15 pm
by AZJIO