News : The level of daily SPAM has reached insane proportions, all registrations are now manual. I ask you to send me an e-mail (john (at) murga (dot) org), to confirm that you want me to create an account for you.


Post Reply  Post Thread 
Listing availiable storage devices
Author Message
dvw86
Junior Member
**


Posts: 39
Group: Registered
Joined: Apr 2007
Status: Offline
Reputation: 0
Post: #1
Listing availiable storage devices

Hi all. It's been quite a while since I've been on here, but I'm back at it. I'm trying to build a list of all availiable internal and external drives that are mounted. It needs to work in Windows and Linux. In Windows it is kind of clumsy but I do have it working with this code.

Code:
--check for windows
CheckForWindows = "C:/"

windows = false

--Define the drive table.        
drive = {}

if lfs.attributes (CheckForWindows) ~= nil then
    
       windows = true

       --Check for all the Windows drives.
       if lfs.attributes ("A:/") ~= nil then
                drive["a"] = "A:"
       end

   -- Use the above code to check for each drive letter from A to Z.

end


In linux, my first thought was to just check the /mnt directory. However I found that some types of Linux don't use this directory. For instance my computer running PCLinuxOS has all removable media listed in the "/Media" directory and the "/mnt" directory is empty. I can see this could end up being really messy. Is there an easier way to do this?

Thanks,
Dan

12-27-2010 05:59 AM
Find all posts by this user Quote this message in a reply
jpjacobs
Member
***


Posts: 113
Group: Registered
Joined: Jul 2007
Status: Offline
Reputation: 0
Post: #2
RE: Listing availiable storage devices

For linux, I'd try to parse the output of the 'mount' command.
Like this more or less:

Code:
fh=io.popen("mount","r")
output = fh:read('*a')
for dev,point in output:gmatch("(/dev/[%w_/]+) on (/[%w_/]*).-\n") do
    print('--',dev,point)
end

12-27-2010 10:52 PM
Find all posts by this user Quote this message in a reply
mikshaw
Senior Member
****


Posts: 522
Group: Registered
Joined: Apr 2007
Status: Offline
Reputation: 5
Post: #3
RE: Listing availiable storage devices

Another Linux option is to check /etc/mtab, which is a list of all currently mounted filesystems.

Code:
for line in io.lines("/etc/mtab") do
if string.find(line,"^/dev/") then
  dev,_=string.gsub(line,"%s.*","") -- cut off everything after field 1
  print(dev)
end
end


or a more compact version that does similar...

Code:
for line in io.lines("/etc/mtab") do
  print(string.match(line,"^(/dev/%w+).*"))
end

This post was last modified: 12-28-2010 10:25 AM by mikshaw.

12-28-2010 10:16 AM
Find all posts by this user Quote this message in a reply
dvw86
Junior Member
**


Posts: 39
Group: Registered
Joined: Apr 2007
Status: Offline
Reputation: 0
Post: #4
RE: Listing availiable storage devices

Hey thanks!! Those all worked very well. I will give them a try in a few differant versions of Linux and wok it into my application.

12-28-2010 12:52 PM
Find all posts by this user Quote this message in a reply
Post Reply  Post Thread 

View a Printable Version
Send this Thread to a Friend
Subscribe to this Thread | Add Thread to Favorites

Forum Jump: