Mounted Drives

Post Reply
User avatar
cogier
Site Admin
Posts: 507
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Mounted Drives

Post by cogier » Wednesday 01st January 2020 5:06pm

Following a comment by Sholzy regarding moving a cache to another drive, I started looking into ways to access all mounted drives and as a start came up with the code below. As usual I am looking for feedback. Does this code work on your system?

All you need to do is paste the code into a 'Graphical Application' and run it. All it does is look at the drives and display the drive you click on in a DirChooser. There is a timer so that if you plug in (or remove) a USB drive /Mount (or unmount) a drive it should change the display. It will only display mounted drives.

I look forward to your comments.
' Gambas class file

sDriveToWorkWith As New String[]
HSplit1 As HSplit
DirChooser1 As DirChooser
GridViewDrives As GridView
LabelPath As Label
TimerGetData As Timer
siChange As Short

Public Sub Form_Open()

  BuildForm
  HSplit1.Layout = [70, 30]
  TimerGetData_Timer
  TimerGetData.Start

End

Public Sub GridViewDrives_Click()

  Dim siRow, siCol As Short

  DirChooser1.Root = GridViewDrives[Last.Row, 6].Text

  For siRow = 0 To GridViewDrives.Rows.Max
    For siCol = 0 To 6
      If siRow <> Last.Row Then
        GridViewDrives[siRow, siCol].Background = Color.Default
      Else
        GridViewDrives[siRow, siCol].Background = Color.LightGray
      End If
    Next
  Next

End

Public Sub DisplayData()

  Dim sHeader As String[] = ["Drive", "Type", "Size", "Used", "Available", "Used", "Mounted"]
  Dim siRow, siCol As Short

  With GridViewDrives
    .Clear
    .Columns.Count = 7
    .Rows.Count = 0
  End With

  For siCol = 0 To 6
    GridViewDrives.Columns[siCol].Title = sHeader[siCol]
  Next

  For siRow = 0 To sDriveToWorkWith.Max
    Inc GridViewDrives.Rows.Count
    For siCol = 0 To 6
      GridViewDrives[siRow, siCol].Text = Split(sDriveToWorkWith[siRow], "`")[siCol]
    Next
  Next

  GridViewDrives.Columns.Width = -1
  GridViewDrives.Height = (GridViewDrives.Rows[0].Height * sDriveToWorkWith.Count) + 35

End

Public Sub TimerGetData_Timer()

  Dim sData, sHold, sPart1, sPart2 As String
  Dim sDrives As String[]
  Dim siLoop, siSplit As Short

  siChange = sDriveToWorkWith.Count
  sDriveToWorkWith.Clear

  Shell "df -aTh" To sData
  sDrives = Split(sData, gb.NewLine, "", True)

  For siLoop = 0 To sDrives.Max

    If sDrives[siLoop] Begins "/dev/s" Then
      sPart2 = Mid(sDrives[siLoop], InStr(sDrives[siLoop], "/", 6))
      sPart1 = Mid(sDrives[siLoop], 1, InStr(sDrives[siLoop], "/", 6) - 1)
      sData = ""

      For siSplit = 0 To Split(sPart1, " ").Max
        sData &= Split(sPart1, " ")[siSplit] & "`"
      Next

      Repeat
        sHold = sData
        sData = Replace(sData, "``", "`")
      Until sData = sHold

      If sData Ends "`" Then sData = Left(sData, -1)
      sDriveToWorkWith.Add(sData & "`" & sPart2)
    Endif

  Next

  If siChange <> sDriveToWorkWith.Count Then DisplayData

End

Public Sub DirChooser1_Change()

  LabelPath.Text = DirChooser1.SelectedPath

End

Public Sub BuildForm()

  With Me
    .Height = 575
    .Width = 1100
    .Arrangement = Arrange.Vertical
    .Padding = 5
  End With

  With HSplit1 = New HSplit(Me)
    .Expand = True
    .Layout = [70, 30]
  End With

  With GridViewDrives = New GridView(HSplit1) As "GridViewDrives"
    .Header = GridView.Both
    .Padding = 2
    .AutoResize = True
  End With

  With DirChooser1 = New DirChooser(HSplit1) As "DirChooser1"
    .Expand = True
  End With

  With LabelPath = New Label(Me) As "LabelPath"
    .Alignment = Align.Right
    .Font.Bold = True
    .Height = 28
  End With

  With TimerGetData = New Timer As "TimerGetData"
    .delay = 250
  End With

End

User avatar
Got2BeFree
Posts: 88
Joined: Saturday 26th November 2016 2:52am
Location: Lost

Re: Mounted Drives

Post by Got2BeFree » Thursday 02nd January 2020 1:41am

The code as is wasn't able to find my Raid partitions. Raid partitions show up as /dev/md[x].

One small change to line 79 allowed your code to find all my partitions, including the Raid partitions.
If sDrives[siLoop] Begins "/dev/s" Then
changed to:
If sDrives[siLoop] Begins "/dev/" Then
sholzy

I'm wondering around lost in the past, not knowing where the present is.

User avatar
cogier
Site Admin
Posts: 507
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: Mounted Drives

Post by cogier » Thursday 02nd January 2020 5:57pm

If sDrives[siLoop] Begins "/dev/" Then
This solved one issue but unfortunately I ended up with all sorts of irrelevant stuff so have a go with this code.
' Gambas class file

sDriveToWorkWith As New String[]
HSplit1 As HSplit
DirChooser1 As DirChooser
GridViewDrives As GridView
LabelPath As Label
TimerGetData As Timer
siChange As Short

Public Sub Form_Open()

  BuildForm
  TimerGetData_Timer
  TimerGetData.Start

End

Public Sub GridViewDrives_Click()

  Dim siRow, siCol As Short

  DirChooser1.Root = GridViewDrives[Last.Row, 6].Text

  For siRow = 0 To GridViewDrives.Rows.Max
    For siCol = 0 To 6
      If siRow <> Last.Row Then
        GridViewDrives[siRow, siCol].Background = Color.Default
      Else
        GridViewDrives[siRow, siCol].Background = Color.LightGray
      End If
    Next
  Next

End

Public Sub DisplayData()

  Dim sHeader As String[] = ["Drive", "Type", "Size", "Used", "Available", "Used", "Mounted"]
  Dim siRow, siCol As Short

  With GridViewDrives
    .Clear
    .Columns.Count = 7
    .Rows.Count = 0
  End With

  For siCol = 0 To 6
    GridViewDrives.Columns[siCol].Title = sHeader[siCol]
  Next

  For siRow = 0 To sDriveToWorkWith.Max
    Inc GridViewDrives.Rows.Count
    For siCol = 0 To 6
      GridViewDrives[siRow, siCol].Text = Split(sDriveToWorkWith[siRow], "`")[siCol]
    Next
  Next

  GridViewDrives.Columns.Width = -1
  GridViewDrives.Height = (GridViewDrives.Rows[0].Height * sDriveToWorkWith.Count) + 35

End

Public Sub TimerGetData_Timer()

  Dim sData, sHold, sPart1, sPart2, sCtrl As String
  Dim sDrives As String[]
  Dim siLoop, siSplit As Short

  siChange = sDriveToWorkWith.Count
  sDriveToWorkWith.Clear

  Shell "df -aTh" To sData
  sDrives = Split(sData, gb.NewLine, "", True)

  For siLoop = 0 To sDrives.Max

    If sDrives[siLoop] Begins "/dev/" Then

      sCtrl = Trim(Split(sDrives[siLoop], " ")[0])
      If Len(sCtrl) <> 9 Or Not IsNumber(Right(sCtrl)) Then Continue

      sPart2 = Mid(sDrives[siLoop], InStr(sDrives[siLoop], "/", 6))
      sPart1 = Mid(sDrives[siLoop], 1, InStr(sDrives[siLoop], "/", 6) - 1)
      sData = ""

      For siSplit = 0 To Split(sPart1, " ").Max
        sData &= Split(sPart1, " ")[siSplit] & "`"
      Next

      Repeat
        sHold = sData
        sData = Replace(sData, "``", "`")
      Until sData = sHold

      If sData Ends "`" Then sData = Left(sData, -1)
      sDriveToWorkWith.Add(sData & "`" & sPart2)
    Endif

  Next

  If siChange <> sDriveToWorkWith.Count Then DisplayData

End

Public Sub DirChooser1_Change()

  LabelPath.Text = DirChooser1.SelectedPath

End

Public Sub BuildForm()

  With Me
    .Height = 575
    .Width = 1100
    .Arrangement = Arrange.Vertical
    .Padding = 5
  End With

  With HSplit1 = New HSplit(Me)
    .Expand = True
    .Layout = [70, 30]
  End With

  With GridViewDrives = New GridView(HSplit1) As "GridViewDrives"
    .Header = GridView.Both
    .Padding = 2
    .AutoResize = True
  End With

  With DirChooser1 = New DirChooser(HSplit1) As "DirChooser1"
    .Expand = True
  End With

  With LabelPath = New Label(Me) As "LabelPath"
    .Alignment = Align.Right
    .Font.Bold = True
    .Height = 28
  End With

  With TimerGetData = New Timer As "TimerGetData"
    .delay = 250
  End With

End

User avatar
Got2BeFree
Posts: 88
Joined: Saturday 26th November 2016 2:52am
Location: Lost

Re: Mounted Drives

Post by Got2BeFree » Thursday 02nd January 2020 10:28pm

Same as before, didn't find the /dev/md[x] Raid partitions.

Here's the output of df if it helps any.

Code: Select all

$ df -aTh
Filesystem     Type         Size  Used Avail Use% Mounted on
sysfs          sysfs           0     0     0    - /sys
proc           proc            0     0     0    - /proc
udev           devtmpfs     7.8G     0  7.8G   0% /dev
devpts         devpts          0     0     0    - /dev/pts
tmpfs          tmpfs        1.6G   58M  1.6G   4% /run
/dev/sdd2      ext4         110G  8.0G   96G   8% /
securityfs     securityfs      0     0     0    - /sys/kernel/security
tmpfs          tmpfs        7.9G   54M  7.8G   1% /dev/shm
tmpfs          tmpfs        5.0M  4.0K  5.0M   1% /run/lock
tmpfs          tmpfs        7.9G     0  7.9G   0% /sys/fs/cgroup
cgroup2        cgroup2         0     0     0    - /sys/fs/cgroup/unified
cgroup         cgroup          0     0     0    - /sys/fs/cgroup/systemd
pstore         pstore          0     0     0    - /sys/fs/pstore
efivarfs       efivarfs        0     0     0    - /sys/firmware/efi/efivars
bpf            bpf             0     0     0    - /sys/fs/bpf
cgroup         cgroup          0     0     0    - /sys/fs/cgroup/devices
cgroup         cgroup          0     0     0    - /sys/fs/cgroup/freezer
cgroup         cgroup          0     0     0    - /sys/fs/cgroup/pids
cgroup         cgroup          0     0     0    - /sys/fs/cgroup/perf_event
cgroup         cgroup          0     0     0    - /sys/fs/cgroup/cpu,cpuacct
cgroup         cgroup          0     0     0    - /sys/fs/cgroup/rdma
cgroup         cgroup          0     0     0    - /sys/fs/cgroup/memory
cgroup         cgroup          0     0     0    - /sys/fs/cgroup/net_cls,net_prio
cgroup         cgroup          0     0     0    - /sys/fs/cgroup/cpuset
cgroup         cgroup          0     0     0    - /sys/fs/cgroup/blkio
systemd-1      -               -     -     -    - /proc/sys/fs/binfmt_misc
mqueue         mqueue          0     0     0    - /dev/mqueue
debugfs        debugfs         0     0     0    - /sys/kernel/debug
hugetlbfs      hugetlbfs       0     0     0    - /dev/hugepages
/dev/sde2      ext4         720G  516M  682G   1% /Recovery
/dev/md0       ext4         196G   62M  186G   1% /Shared
/dev/md1       ext4         720G  160G  524G  24% /Storage
/dev/sdd1      vfat         285M  5.1M  280M   2% /boot/efi
/dev/sde3      ext4          55G  3.4G   49G   7% /home
tmpfs          tmpfs        1.6G   12K  1.6G   1% /run/user/1000
fusectl        fusectl         0     0     0    - /sys/fs/fuse/connections
tmpfs          tmpfs        1.6G     0  1.6G   0% /run/user/0
sunrpc         rpc_pipefs      0     0     0    - /run/rpc_pipefs
nfsd           nfsd            0     0     0    - /proc/fs/nfsd
binfmt_misc    binfmt_misc     0     0     0    - /proc/sys/fs/binfmt_misc
sholzy

I'm wondering around lost in the past, not knowing where the present is.

Post Reply