List all USB drives to combobox

Post

Posted
Rating:
#1 (In Topic #1354)
Regular
theyikes is in the usergroup ‘Regular’
Ok before i begin I just want to promise I WILL reply to any posts you may offer. It was brought to my attention that i was forgetting about replying. Ok that said I'll continue. Well I want to list all physical usb drives in a combobox.  I found this commadn
lsblk -o NAME,FSTYPE | awk '$2 == "" { print $0 }'
which gives me the exact results. However I can't figure out how to incorporate it into a combobox. I did it with printers and that was great but try as i might i can't figure this one out. I SWEAR I took some time to work this one out, I'm not just running to this forum every time  i encounter a problem.
Anyway is this doable ?
Thanks in advance!
Online now: No Back to the top

Post

Posted
Rating:
#2
Regular
theyikes is in the usergroup ‘Regular’
 I tried running the above mentioned command as shell but I does'nt give me any output. However the command lsblk | grep disk gives me something close to what i want.
Online now: No Back to the top

Post

Posted
Rating:
#3
Regular
theyikes is in the usergroup ‘Regular’
 Hey guys. I don't mean to sound strange but have i been blacklisted because i didn't reply?
Online now: No Back to the top

Post

Posted
Rating:
#4
Guru
BruceSteers is in the usergroup ‘Guru’
No , and once again my harmless dig was not about you replying but it was about you asking a question that had been answered in the previous post you made.

So it was about you "NOT reading the replies, nothing to do with if you had replied or not.

I did a google search on how to detect usb disks for you and came up with this after seeing that TRAN can define the usb disks.
So i make a shell command that runs lsblk to get the disk information of the usb disks it detects with it's previous lsblk call
to detect usb drives…
lsblk -o PATH,TRAN| grep usb


Code (gambas)

  1.  
  2. Dim sReturn As String
  3. ' get a list of usb disks with size
  4. Shell "IFS=$'\n'; for s in $(lsblk -o PATH,TRAN| grep usb); do lsblk -o 'MOUNTPOINT,SIZE' $\{s%% *}|grep /; done" To sReturn
  5.  
  6. ' split the list as a String[] to the Conbobox.List property
  7. ComboBox1.List = Split(sReturn, "\n", Null, True)
  8.  
  9.  
Online now: No Back to the top

Post

Posted
Rating:
#5
Regular
theyikes is in the usergroup ‘Regular’
 Hey brucesteers, sorry if i came across as over sensitive. It's in my nature. That command is great. It's better than what I've been getting.I've tried putting what i know about adding printers to a listbox but I'm coming up blank. Could you perhaps maybe help me out AGAIN!!!
Online now: No Back to the top

Post

Posted
Rating:
#6
Regular
theyikes is in the usergroup ‘Regular’
 sorry BruceSteers i didn't see the code you gave me. I'll try it out right now and I SWEAR i'll get back to you.
Online now: No Back to the top

Post

Posted
Rating:
#7
Regular
theyikes is in the usergroup ‘Regular’
Hello again! Ok i tried what you gave me
Dim sReturn As String
Shell "IFS=$'\n'; for s in $(lsblk -o PATH,TRAN| grep usb); do lsblk -o 'MOUNTPOINT,SIZE' ${s%% *}|grep /; done" To sReturn
ComboBox1.List = Split(sReturn, "\n", Null, True)
but nothing's coming up in the combobox
I tried replacing the shell command with something different but still coming up empty.

I apologize if I@m doing your head in.
Online now: No Back to the top

Post

Posted
Rating:
#8
Guru
BruceSteers is in the usergroup ‘Guru’

theyikes said

I've tried putting what i know about adding printers to a listbox but I'm coming up blank. Could you perhaps maybe help me out AGAIN!!!

For printers you can use..

Printer.List  (gb.form.print)
/comp/gb.qt4/printer - Gambas Documentation

Code (gambas)

  1. ListBox1.List=Printer.List
  2.  
Online now: No Back to the top

Post

Posted
Rating:
#9
Guru
BruceSteers is in the usergroup ‘Guru’

theyikes said

Hello again! Ok i tried what you gave me
Dim sReturn As String
Shell "IFS=$'\n'; for s in $(lsblk -o PATH,TRAN| grep usb); do lsblk -o 'MOUNTPOINT,SIZE' ${s%% *}|grep /; done" To sReturn
ComboBox1.List = Split(sReturn, "\n", Null, True)
but nothing's coming up in the combobox
I tried replacing the shell command with something different but still coming up empty.

I apologize if I@m doing your head in.

Works fine here , it lists ALL my plugged in usb disk drives.
I take it you have usb disks plugged in?
Online now: No Back to the top

Post

Posted
Rating:
#10
Regular
theyikes is in the usergroup ‘Regular’
BruceSteers I think i have it. I modded what you agve me and i ran it and i pretty much got what i was looking for. Here's what i came up with:

Dim sReturn As String
Shell "lsblk | grep disk" To sReturn
ListBox1.Add(sReturn)

It's almost 100% accurate.

Thanks A MILLION for your help, Honestly there's isn't a snowballs chance in hell i could have done it without you.

Who knows maybe sometime in the distant future someone will stumble upon this and learn from it.
Cheers
Online now: No Back to the top

Post

Posted
Rating:
#11
Regular
theyikes is in the usergroup ‘Regular’
 oops! i was wrong, the code i mentioned only seems to add all the drives as one item in the listbox. it doesn't seem to separate them as multiple dirves. I was so happy, i thought I FINALLY got it,,  I guess i got too happy too soon.
Online now: No Back to the top

Post

Posted
Rating:
#12
Guru
BruceSteers is in the usergroup ‘Guru’

theyikes said

BruceSteers I think i have it. I modded what you agve me and i ran it and i pretty much got what i was looking for. Here's what i came up with:

Dim sReturn As String
Shell "lsblk | grep disk" To sReturn
ListBox1.Add(sReturn)

It's almost 100% accurate.

Thanks A MILLION for your help, Honestly there's isn't a snowballs chance in hell i could have done it without you.

Who knows maybe sometime in the distant future someone will stumble upon this and learn from it.
Cheers

So you wasn't actually wanting USB drives like you asked for , you just wanted a list of all disks.

Yes let's hope they learn all they need to know.

you havent "modded" my code you just wrote your own, and your code is only adding one item , the entire list as one string.
Shell "lsblk | grep disk" To sReturn
ListBox1.Add(sReturn)

is that almost 100% accurate?

this would be what you want like in my previous code example before you wrote your own.
ListBox1.List = Split(sReturn,"\n", Null, True)
Online now: No Back to the top

Post

Posted
Rating:
#13
Regular
theyikes is in the usergroup ‘Regular’
Ok that worked PERFECTLY !!!! I have to ask, how long have you been using Gambas?
Online now: No Back to the top

Post

Posted
Rating:
#14
Guru
BruceSteers is in the usergroup ‘Guru’

theyikes said

Ok that worked PERFECTLY !!!! I have to ask, how long have you been using Gambas?

Right now it feels like too long  …   ;)  :lol:

I don't know , it was gambas2 when I originally started.
Online now: No Back to the top

Post

Posted
Rating:
#15
Regular
theyikes is in the usergroup ‘Regular’
 Man i wish i knew half of what you know. I'd love to be the one answering questions instead of asking them.
Online now: No Back to the top

Post

Posted
Rating:
#16
Regular
theyikes is in the usergroup ‘Regular’
Okay! This worked:

  Dim sReturn As String
Shell " lsblk –nodeps –paths –output NAME,TRAN | grep usb | awk '{print $1}' " To sReturn
 ListBox1.List = Split(sReturn, "\n", Null, True)

it just gives me the list of physical drives without any text.

Thanks guys!!!
Online now: No Back to the top

Post

Posted
Rating:
#17
Regular
vuott is in the usergroup ‘Regular’
If you did not want to use “Shell”, you could use the resources of the external library “libudev”.

This code will enumerate all devices belonging to 'block' subsystem and omit partitions and loop devices:

Code (gambas)

  1. Library "libudev:1.7.9"   ' ...or more simply: Library "libudev"
  2.  
  3. ' struct udev *udev_new(void)
  4. ' Create udev library context.
  5.  
  6. ' struct udev_enumerate *udev_enumerate_new(struct udev *udev)
  7. ' Create, acquire and release a udev enumerate object.
  8. Private Extern udev_enumerate_new(udev As Pointer) As Pointer
  9.  
  10. ' int udev_enumerate_add_match_subsystem(struct udev_enumerate *udev_enumerate, const char *subsystem)
  11. ' Filter for a subsystem of the device to include in the list.
  12. Private Extern udev_enumerate_add_match_subsystem(udev_enumerate As Pointer, subsystem As String) As Integer
  13.  
  14. ' int udev_enumerate_scan_devices(struct udev_enumerate *udev_enumerate)
  15. ' Query or modify a udev enumerate object.
  16. Private Extern udev_enumerate_scan_devices(udev_enumerate As Pointer) As Integer
  17.  
  18. ' struct udev_list_entry *udev_enumerate_get_list_entry(struct udev_enumerate *udev_enumerate)
  19. ' Query or modify a udev enumerate object.
  20. Private Extern udev_enumerate_get_list_entry(udev_enumerate As Pointer) As Pointer
  21.  
  22. ' const char *udev_list_entry_get_name(struct udev_list_entry *list_entry)
  23. ' Iterate and access udev lists.
  24. Private Extern udev_list_entry_get_name(list_entry As Pointer) As String
  25.  
  26. ' struct udev_device *udev_device_new_from_syspath(struct udev *udev, const char *syspath)
  27. ' Create, acquire and release a udev device object.
  28. Private Extern udev_device_new_from_syspath(udev As Pointer, syspath As String) As Pointer
  29.  
  30. ' const char *udev_device_get_devtype(struct udev_device *udev_device)
  31. ' Return a pointer to a constant string that describes the requested property.
  32. Private Extern udev_device_get_devtype(udev_device As Pointer) As String
  33.  
  34. ' const char *udev_device_get_devnode(struct udev_device *udev_device)
  35. ' Returns the path to the device node itself in /dev.
  36. Private Extern udev_device_get_devnode(udev_device As Pointer) As String
  37.  
  38. ' const char *udev_device_get_sysname(struct udev_device *udev_device)
  39. ' Return a pointer to a constant string that describes the requested property.
  40. Private Extern udev_device_get_sysname(udev_device As Pointer) As String
  41.  
  42. ' const char *udev_device_get_devpath(struct udev_device *udev_device)
  43. ' Return a pointer to a constant string that describes the requested property.
  44. Private Extern udev_device_get_devpath(udev_device As Pointer) As String
  45.  
  46. ' const char *udev_device_get_sysattr_value(struct udev_device *udev_device, const char *sysattr)
  47. ' Return a pointer to a constant string that describes the requested property.
  48. Private Extern udev_device_get_sysattr_value(udev_device As Pointer, sysattr As String) As String
  49.  
  50. ' struct udev_device *udev_device_unref(struct udev_device *udev_device)
  51. ' Drop a reference of a udev device.
  52. Private Extern udev_device_unref(udev_device As Pointer) As Pointer
  53.  
  54. ' struct udev_list_entry *udev_list_entry_get_next(struct udev_list_entry *list_entry)
  55. ' Iterate and access udev lists.
  56. Private Extern udev_list_entry_get_next(list_entry As Pointer) As Pointer
  57.  
  58. ' struct udev_enumerate *udev_enumerate_unref(struct udev_enumerate *udev_enumerate)
  59. ' Drop a reference of an enumeration context.
  60. Private Extern udev_enumerate_unref(udev_enumerate As Pointer) As Pointer
  61.  
  62. ' struct udev *udev_unref(struct udev *udev)
  63. ' Drop a reference of the udev library context.
  64. Private Extern udev_unref(udev As Pointer) As Pointer
  65.  
  66.  
  67. Public Sub Main()
  68.  
  69.  Dim udev, enu, lst, dev As Pointer
  70.  Dim path, tmp As String
  71.  Dim disk_size As Long
  72.  Dim block_size As Short = 512
  73.  
  74.  udev = udev_new()
  75.  If udev == 0 Then Error.Raise("Error !")
  76.  
  77.  enu = udev_enumerate_new(udev)
  78.  If enu == 0 Then Error.Raise("Error !")
  79.  
  80.  udev_enumerate_add_match_subsystem(enu, "block")
  81.  udev_enumerate_scan_devices(enu)
  82.  
  83.  lst = udev_enumerate_get_list_entry(enu)
  84.  If lst == 0 Then Error.Raise("Error !")
  85.  
  86.    path = udev_list_entry_get_name(lst)
  87.    dev = udev_device_new_from_syspath(udev, path)
  88.    If (udev_device_get_devtype(dev) <> "partition") And (udev_device_get_sysname(dev) <> "loop") Then
  89.      Print "I: DEVNODE= "; udev_device_get_devnode(dev)
  90.      Print "I: KERNEL=  "; udev_device_get_sysname(dev)
  91.      Print "I: DEVPATH= "; udev_device_get_devpath(dev)
  92.      Print "I: DEVTYPE= "; udev_device_get_devtype(dev)
  93.      tmp = udev_device_get_sysattr_value(dev, "size")
  94.      If Not IsNull(tmp) Then disk_size = Val(tmp)
  95.      tmp = udev_device_get_sysattr_value(dev, "queue/logical_block_size")
  96.      If Not IsNull(tmp) Then block_size = Val(tmp)
  97.      If udev_device_get_sysname(dev) <> "sr" Then
  98.        Print "I: DEVSIZE= "; Fix((disk_size * block_size) / 1000000000); " GB"
  99.      Endif
  100.    Endif
  101.    udev_device_unref(dev)
  102.    lst = udev_list_entry_get_next(lst)
  103.    Print
  104.  Until lst == 0
  105.  
  106.  udev_enumerate_unref(enu)
  107.  udev_unref(udev)
  108.  

Europaeus sum !

<COLOR color="#FF8000">Amare memorentes atque deflentes ad mortem silenter labimur.</COLOR>
Online now: No Back to the top
1 guest and 0 members have just viewed this.