?
Solved

Checking if a drive is ready for access?

Posted on 1998-11-01
8
Medium Priority
?
206 Views
Last Modified: 2010-04-06
What is the best way to test if a drive is ready for access? This is needed for drives like cd-roms and jaz drives.

Thanks!
0
Comment
Question by:Kmiller
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
8 Comments
 
LVL 3

Expert Comment

by:philipleighs
ID: 1345316
Hi,

Here is a snippet of code that may help.
The important point is the SetErrorMode calls. Without these, (and when running outside Delphi), and when the a: drive is not ready you get a message saying "System error, drive not ready, Abort, Retry, Cancel.
The SetErrorMode supresses these error messages.
You can then call DiskFree. There may be something better than DiskFree, but if it returns -1 then the drive is not ready.

PrevErrorMode := SetErrorMode(SEM_FAILCRITICALERRORS);
try
   if DiskFree(1) <> -1 then
      //A: Drive is ready
   else
      //A: drive is not ready
finally
  SetErrorMode(PrevErrorMode);
end;

0
 

Author Comment

by:Kmiller
ID: 1345317
how would this be done when you only have the drive letter? Say M:\ which is a cdrom drive.
0
 
LVL 3

Expert Comment

by:philipleighs
ID: 1345318
//'A' or 'a' returns 1,
//'B' or 'b' returns 2 etc
function GetDriveNumber(Letter: Char): Integer;
begin
  Result := Ord(UpCase(Letter)) - Ord('A') + 1;
end;


begin
DiskFree(   GetDriveNumber('M')  );
.

Note if you have a string, you could also use GetDriveNumber like this:

s := 'M:\';
DiskFree(   GetDriveNumber(  s[1]  );

Cheers,
Phil.

PS: Tell me if you want code that tells you which drives(s) are CD-ROMs then I'll can paste it here.

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Expert Comment

by:dwwang
ID: 1345319
This one is from Inprise's FAQ, it does not need to use try..except.

function IsDriveReady(DriveLetter : char) : bool;
                    var
                      OldErrorMode : Word;
                      OldDirectory : string;
                    begin
                      OldErrorMode := SetErrorMode(SEM_NOOPENFILEERRORBOX);
                      GetDir(0, OldDirectory);
                      {$I-}
                        ChDir(DriveLetter + ':\');
                      {$I+}
                       if IoResult <> 0 then
                        Result := False
                       else
                        Result := True;

                      ChDir(OldDirectory);
                      SetErrorMode(OldErrorMode);
                    end;
0
 

Author Comment

by:Kmiller
ID: 1345320
Thanks! They both seem to work... someone answer for credit.
0
 
LVL 4

Accepted Solution

by:
dwwang earned 400 total points
ID: 1345321
Sorry for others ^_^
0
 
LVL 3

Expert Comment

by:philipleighs
ID: 1345322
Hmm,

Don't you think the first answer is the one that deserves the credit?
0
 
LVL 4

Expert Comment

by:dwwang
ID: 1345323
Hi, I just happened to be notified by e-mail and come here to make an answer, if you care of that, now I just ask Kmiller to reject my answer.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses
Course of the Month8 days, 16 hours left to enroll

764 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