Visit our Sponsor   Visit our Sponsor
delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
Have a look at your member-status

connecting people's knowledge


  - Recent ArticlesRSS feed for Recent Articles on delphi3000.com
  - List of All Articles
  - Top Viewed Articles
  - Articles (+Attachem.)
  - Articles Of Interest
  - Categories
  - Top Uploader
  - Search
  - Index

  - My Home
  - Submit an Article
  - My Articles
  - My Personal Data
  - My Bookmarks
  - Activities
  - Login/Logout

  - Sign Up
  - Why Sign Up
  - Newsletter

  - Press
  - Advertise

  - Contact
  - Feedback





Community
Borland
ClubeDelphi
Dr. Bob
UK-BUG
Delphi Meetings
Planeta Delphi







Startblatt.de






Share this article with friendsShare this article with friends
Rate this articleRate this article - to keep the quality of delphi3000.com !
Comment this article or read through previous comments (3)


maping/unmaping virtual drivesFormat this article printer-friendly!Bookmark function is only available for registered users!
tired of navigating to a location deep on your C drive?
Product:
Delphi all versions
Category:
System
Skill Level:
Scoring:
Last Update:
05/10/2002
Search Keys:
delphi delphi3000 article borland vcl code-snippet virtual drive virtual-drives SUBST DOS ms-dos
Times Scored:
4
Visits:
5959
Uploader: Eber Irigoyen
Company: BTXSys
Reference: N/A
 
Question/Problem/Abstract:
Have you ever got tired of clicking in Explorer for that file that's in the deepest tree-node?.
at least:
c:\program files\borland\delphi5\projects\...
Answer:



I've created two functions that will let you create Virtual Drives for your most commonly used folders
(local hard drive folders like c:\program files\borland\delphi5\projects or even network folders, like f:\myapplications\utils),
so it will appear on "My Computer" as a new drive and you can access it very quickly.

The elder Delphians-programmers will remember the good old DOS-days where we had a command called SUBST... well... we still have it!

here are the functions:

Procedure UnmapVirtualDrive(Const Drive:Char);
Begin
  If (GetDriveType(PChar(Drive+':\'))<>DRIVE_FIXED) Then
    Raise Exception.Create('['+Drive+':\] Is not a Virtual drive');

  WinExec(PChar('subst /d '+Drive+': '), SW_HIDE);
  Sleep(250); //give some time for the execution
  If (GetDriveType(PChar(Drive+':\'))>1) Then
    Raise Exception.Create('Could not unmap Virtual drive ['+Drive+':\]')
End;

Function MapVirtualDrive(Const Drive:Char; Const Path:String):Boolean;
Var DType:Integer;
Begin
  DType:=GetDriveType(PChar(Drive+':\'));
  If (DType=DRIVE_FIXED) Then
  Begin
    If (MessageDlg('Drive ['+Drive+':\] already exists, Do you want to try And replace it?', mtWarning, [mbYes, mbNo, mbCancel], 0)=mrYes) Then
      UnmapVirtualDrive(Drive)
  End
  Else If (DType>1) Then
    Raise Exception.Create('Drive '+Drive+':\  cannot be maped (drive not overwritable)');

  WinExec(PChar('subst '+Drive+': '+Path), SW_HIDE);
  Sleep(250); //give some time for the execution
  Application.ProcessMessages;
  Result:=GetDriveType(PChar(Drive+':\'))=DRIVE_FIXED
End;

- the MapVirtualDrive function accepts a Char, which will be the new drive, and the path to where it will point to... it returns true if succesful

- the UnmapVirtualDrive also accepts a Char, which will be the drive you need to unmap; the procedure tries to unmap the drive and returns an exception if any error happens

ok... now you want an example... let's see...
drop two TEdits on your form and two TButtons
on the first Edit put the letter you want for your new drive, and in the Edit2 put the path to where it will point:

on the onclick of the first button put this:

Procedure TForm1.Button1Click(Sender: TObject);
Begin
  If (MapVirtualDrive(Edit1.Text[1], Edit2.Text)) Then
    ShowMessage('Virtual Drive ['+Edit1.Text+'] created')
  Else
    ShowMessage('Could not map Virtual Drive ['+Edit1.Text+']')
End;

on the onclick of the second button put this:
Procedure TForm1.Button3Click(Sender: TObject);
Begin
  UnmapVirtualDrive(Edit1.Text[1]);
  ShowMessage(Edit1.Text+' unmaped succesful')
End;

and that's it... you have a button to map, and the other to unmap... you can open "my computer" and see your new drives there... or... here's procedure that will show all your drives in a Memo:

Procedure ListDrives(DrivesStrs:TStrings);
Var
  Drive: Char;
  DriveLetter: String[4];
  sDriveType: String;
Begin
  For Drive := 'A' To 'Z' Do
  Begin
    DriveLetter := Drive + ':\';
    sDriveType := '';
    Case GetDriveType(PChar(Drive + ':\')) Of
      DRIVE_REMOVABLE:  sDriveType := 'Floppy';
      DRIVE_FIXED:      sDriveType := 'Fixed';
      DRIVE_REMOTE:     sDriveType := 'Network';
      DRIVE_CDROM:      sDriveType := 'CD-ROM';
      DRIVE_RAMDISK:    sDriveType := 'RAM Disk';
    End;
    If sDriveType <> '' Then
    Begin
      sDriveType := sDriveType + ' Drive ';
      DrivesStrs.Add(Format('Drive %s:', [Drive])+' - '+sDriveType);
    End;
  End;
End;

and you could call it just like:
ListDrives(MyMemo.Lines);

or... you know, any component that uses TStrings

I hope you find these functions useful

The source code of the article was formatted using my:
PAS 2 HTML converter

keep up coding!

salu2

EberSys





Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment
Virtual Drive
    Andy West (May 21 2002 3:40PM)

Excellent little tool, I had to use the following path to map to my delphi projects.
C:\Progra~1\Borland\Delphi5\Projects
Andy
Respond

nice
    Jamie Ingilby (May 10 2002 9:37AM)

Good Article , very well explained and easy to understand
Respond

RE: nice
Eber Irigoyen (May 10 2002 10:45PM)

thanks =o)
as the bicentennial man would say:
"one is glad to be of service" ;-)

EberSys
Respond














 
Sign up to consume product discounts for Bronze memberships !

read more


  Visit our Sponsor

 

  Community Ad of
L. Rosenstein
 
   














 







     
  Copyright © 2000 - 2007 delphi3000.com - All rights reserved. Terms of use. || Privacy
delphi3000.com is a service by bluestep.com IT-Services GmbH (Vienna)