Drive Letters

Hints, tips and tricks for newbies

Moderators: Dorian (MJT support), JRL

Post Reply
lcrltd
Junior Coder
Posts: 35
Joined: Thu Nov 16, 2006 8:23 am

Drive Letters

Post by lcrltd » Fri Aug 17, 2007 10:05 am

Hi, can someone tell me how call drive letters, for example if i want to refer to files on a removable disk i would need something like %drive% as the disk might have a different letter on different PC's.

Many thanks
:)

edauthier
Pro Scripter
Posts: 84
Joined: Sun Apr 13, 2003 1:26 pm
Location: USA

Post by edauthier » Fri Aug 17, 2007 4:23 pm

I am not clear how exactly I would do this from MS, but you could integrate with a VBScript and call the results into your MS script.

Open Notepad and paste the code below into it.
Save the document as drives.vbs (be sure to change text file to all files in the save box drop-down)

Run the drives.vbs file by clicking on it.

The Code: (Many Thanks G. Born)

Option Explicit

' Drive type constants
Const Unknown = 0
Const Removable = 1 ' Removable medium
Const Fixed = 2 ' Fixed medium (hard disk)
Const Remote = 3 ' Network drive
Const CDROM = 4 ' CD-ROM
Const RAMDisk = 5 ' RAM disk

Dim Text, Title
Dim fso, oDrive, curDrive ' Object variables

Dim drtype(6)
drtype(0) = " Unknown "
drtype(1) = " Removable "
drtype(2) = " Fixed "
drtype(3) = " Remote "
drtype(4) = " CDROM "
drtype(5) = " RAMDisk "

Text = "Drives" & vbCrLf & vbCrLf
Title = "From Macro Scheduler Post"

' Create FileSystemObject object to access the file system.
Set fso = WScript.CreateObject("Scripting.FileSystemObject")

Set oDrive = fso.Drives ' Get Drives collection.

For Each curDrive In oDrive ' All drive objects
Text = Text & curDrive.DriveLetter & vbTab ' Drive letter
Text = Text & drtype(curDrive.DriveType)
Select Case curDrive.DriveType ' Identify drive type.
Case Removable ' Removable medium
If curDrive.IsReady Then
Text = Text & curDrive.VolumeName ' Local drive
End If

Case CDROM ' CD-ROM
If curDrive.IsReady Then
Text = Text & curDrive.VolumeName ' Local drive
End If

Case Remote
Text = Text & curDrive.ShareName ' Network drive

Case Else ' Other medium
Text = Text & curDrive.VolumeName ' Local drive
End Select
Text = Text & vbCrLf
Next

MsgBox Text, vbOKOnly + vbInformation, Title

'*** End


______

Good Luck, Ed

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Fri Aug 17, 2007 4:33 pm

Macro Scheduler version:

Code: Select all

VBSTART
Option Explicit

Function GetDrives

' Drive type constants
Const Unknown = 0
Const Removable = 1 ' Removable medium
Const Fixed = 2 ' Fixed medium (hard disk)
Const Remote = 3 ' Network drive
Const CDROM = 4 ' CD-ROM
Const RAMDisk = 5 ' RAM disk

Dim Text, Title
Dim fso, oDrive, curDrive ' Object variables

Dim drtype(6)
drtype(0) = " Unknown "
drtype(1) = " Removable "
drtype(2) = " Fixed "
drtype(3) = " Remote "
drtype(4) = " CDROM "
drtype(5) = " RAMDisk "

Text = "Drives" & vbCrLf & vbCrLf
Title = "From Macro Scheduler Post"

' Create FileSystemObject object to access the file system.
Set fso = CreateObject("Scripting.FileSystemObject")

Set oDrive = fso.Drives ' Get Drives collection.

For Each curDrive In oDrive ' All drive objects
  Text = Text & curDrive.DriveLetter & vbTab ' Drive letter
  Text = Text & drtype(curDrive.DriveType)
  Select Case curDrive.DriveType ' Identify drive type.
    Case Removable ' Removable medium
      If curDrive.IsReady Then
        Text = Text & curDrive.VolumeName ' Local drive
      End If

    Case CDROM ' CD-ROM
      If curDrive.IsReady Then
        Text = Text & curDrive.VolumeName ' Local drive
      End If

    Case Remote
      Text = Text & curDrive.ShareName ' Network drive

    Case Else ' Other medium
      Text = Text & curDrive.VolumeName ' Local drive
  End Select
  Text = Text & vbCrLf
Next

GetDrives = Text
End Function
VBEND

VBEval>GetDrives,drive_info
MessageModal>drive_info

Ed, if you're going to post code here could you please try to make it work in Macro Scheduler, so that I don't have to keep rewriting your code ;-)

And also, can you use the Code button when pasting code, to preserve formatting.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

edauthier
Pro Scripter
Posts: 84
Joined: Sun Apr 13, 2003 1:26 pm
Location: USA

Post by edauthier » Fri Aug 17, 2007 4:41 pm

LOL... I know.. might of noticed that I am more of a troll than a contributor. Probably better off for the forum. I'll clean up my act.

User avatar
Bob Hansen
Automation Wizard
Posts: 2475
Joined: Tue Sep 24, 2002 3:47 am
Location: Salem, New Hampshire, US
Contact:

Post by Bob Hansen » Fri Aug 17, 2007 4:44 pm

You could put a unique file on the root of the drive.

Then loop throught the drive letters as variables, seeing IfFileExist>%drive%:\filename.ext

When it exists, then you know the Drive letter that you need...%drive%
Hope this was helpful..................good luck,
Bob
A humble man and PROUD of it!

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Fri Aug 17, 2007 4:44 pm

Not a troll. Your contributions are welcome! Keep contributing!

I just posted a link to the Merlin script on my blog:
http://www.mjtnet.com/blog/
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts