Solved

How to get drive letter/volume label from all connected drives

Posted on 2008-06-20
2
9,481 Views
Last Modified: 2012-06-22
Gents,

I already use the following function to get the volume label using a specific drive letter. I need to be able to scan each connected drive and call this function for each drive. (Unless there is a better way)

Project description:

I am building a video player that works with a database. When I want to play a file, I look in the database to know on which drive the video is stored.

Each drive has got a specific volume label such as BB1, BB2, BB3, ect.

I have built a 4 bay hot swappable HDD USB reader.

Lets say I want to play a file which is stored on BB7.

I need to look through the present HDDs in my reader to see if BB7 is in. If so, play the file, else prompt the user to insert BB7.

In my database, the file name is stored in a string "\Video\myfile.mp2" so I only need to add the right drive letter to the begining of the string in order to pass to my player.

I am not too keen in scanning drive letters from D to Z to look for the label. I would like a cleaner solution, scan through connected drives...

Many thanks.


function GetVolumeLabel(DriveChar: Char): string;

var

  NotUsed:     DWORD;

  VolumeFlags: DWORD;

  VolumeInfo:  array[0..MAX_PATH] of Char;

  VolumeSerialNumber: DWORD;

  Buf: array [0..MAX_PATH] of Char;

begin

    GetVolumeInformation(PChar(DriveChar + ':\'),

    Buf, SizeOf(VolumeInfo), @VolumeSerialNumber, NotUsed,

    VolumeFlags, nil, 0);
 

    SetString(Result, Buf, StrLen(Buf));   { Set return result }

    Result:=AnsiUpperCase(Result)

end;

Open in new window

0
Comment
Question by:etiennedemers
2 Comments
 
LVL 13

Accepted Solution

by:
ThievingSix earned 500 total points
Comment Utility
I know you didn't ask for scanning through all drives, but it takes less time than GetTickCount to update..
function GetVolumeLabel(DriveChar: Char): string;

var

  NotUsed:     DWORD;

  VolumeFlags: DWORD;

  VolumeInfo:  array[0..MAX_PATH] of Char;

  VolumeSerialNumber: DWORD;

  Buf: array [0..MAX_PATH] of Char;

begin

    GetVolumeInformation(PChar(DriveChar + ':\'),

    Buf, SizeOf(VolumeInfo), @VolumeSerialNumber, NotUsed,

    VolumeFlags, nil, 0);

 

    SetString(Result, Buf, StrLen(Buf));   { Set return result }

    Result:=AnsiUpperCase(Result)

end;
 

procedure TForm1.Button1Click(Sender: TObject);

var

  Drive : Char;

  DriveLetter : String;

begin

  For Drive := 'A' To 'Z' Do

    begin

    DriveLetter := Drive + ':\';

    If GetDriveType(PChar(DriveLetter)) = DRIVE_FIXED Then

      begin

      Memo1.Lines.Add(Drive + ': ' + GetVolumeLabel(Drive));

    end;

  end;

end;

Open in new window

0
 

Author Closing Comment

by:etiennedemers
Comment Utility
This will achieve my goals.

Many thanks
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now