Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
MD MD is offline
external usenet poster
 
Posts: 26
Default Identify CD-Rom drive

Using FileCopy, I'd like to copy a file from a CD-Rom that is always in the
same folder to the hard drive.

Sub TranferFiles()
SourceFile = "E:\folder\MyFile.doc"
DestinationFile = "C:\Temp\MyFile.doc"
FileCopy SourceFile, DestinationFile
End Sub

One problem with this....The CD-Rom drive is not always E from one PC to the
other. So how do I identify the CD-Rom drive so that my code looks like
CD_Drive = ???????
SourceFile = CD_Drive + "\folder\MyFile.doc"

Regards!

Michel


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default Identify CD-Rom drive

Michel,

In VBA go to the Tools menu, choose References, then scroll down
to Microsoft Scripting Runtime. Put a check next to this and
click OK. Then, you can use code like the following:

Dim FSO As Scripting.FileSystemObject
Dim Drv As Drive
Set FSO = New Scripting.FileSystemObject
For Each Drv In FSO.Drives
If Drv.DriveType = CDRom Then
Debug.Print Drv.DriveLetter
End If
Next Drv



--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com






"MD" wrote in message
. ..
Using FileCopy, I'd like to copy a file from a CD-Rom that is

always in the
same folder to the hard drive.

Sub TranferFiles()
SourceFile = "E:\folder\MyFile.doc"
DestinationFile = "C:\Temp\MyFile.doc"
FileCopy SourceFile, DestinationFile
End Sub

One problem with this....The CD-Rom drive is not always E from

one PC to the
other. So how do I identify the CD-Rom drive so that my code

looks like
CD_Drive = ???????
SourceFile = CD_Drive + "\folder\MyFile.doc"

Regards!

Michel




  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Identify CD-Rom drive

Michel,

Here is some code that I dapted from my CD-Player that will do it for you.
Got this from Randy Birch originally.

Public Declare Function GetLogicalDriveStrings Lib "kernel32" _
Alias "GetLogicalDriveStringsA" _
(ByVal nBufferLength As Long, _
ByVal lpBuffer As String) As Long

Public Declare Function GetDriveType Lib "kernel32" _
Alias "GetDriveTypeA" _
(ByVal nDrive As String) As Long

Public Const DRIVE_REMOVABLE = 2
Public Const DRIVE_FIXED = 3
Public Const DRIVE_REMOTE = 4
Public Const DRIVE_CDROM = 5
Public Const DRIVE_RAMDISK = 6


Private Sub GetCDDrive()
Dim r As Long
Dim sAll As String
Dim sDrive As String
Dim sType As String

'get the list of all available drives
sAll = GetDriveString()

'separate the drive strings and retrieve the drive type
Do Until sAll = Chr$(0)

sDrive = StripNulls(sAll)

Select Case GetDriveType(sDrive)
Case 0: sType = "The drive type cannot be determined"
Case 1: sType = "The root directory does not exist"

Case DRIVE_REMOVABLE:
Select Case Left$(sDrive, 1)
Case "a", "b": sType = "Floppy drive"
Case Else: sType = "Removable drive"
End Select

Case DRIVE_FIXED: sType = "Hard drive; can not be removed"
Case DRIVE_REMOTE: sType = "Remote (network) drive"
Case DRIVE_CDROM: sType = "CD-ROM drive"
Case DRIVE_RAMDISK: sType = "RAM disk"
End Select

If sType = "CD-ROM drive" Then
MsgBox "Drive " & sDrive & " is " & sType
End If

Loop

End Sub


Private Function GetDriveString() As String
Dim sBuffer As String

'possible 26 drives, three characters each, plus trailing null
sBuffer = Space$(26 * 4)

If GetLogicalDriveStrings(Len(sBuffer), sBuffer) Then
GetDriveString = Trim$(sBuffer)
End If

End Function

Private Function StripNulls(Start As String) As String
Dim iPos As Long

iPos = InStr(Start, Chr$(0))

If iPos Then
StripNulls = Mid$(Start, 1, iPos - 1)
Start = Mid$(Start, iPos + 1, Len(Start))
End If

End Function



--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

"MD" wrote in message
. ..
Using FileCopy, I'd like to copy a file from a CD-Rom that is always in

the
same folder to the hard drive.

Sub TranferFiles()
SourceFile = "E:\folder\MyFile.doc"
DestinationFile = "C:\Temp\MyFile.doc"
FileCopy SourceFile, DestinationFile
End Sub

One problem with this....The CD-Rom drive is not always E from one PC to

the
other. So how do I identify the CD-Rom drive so that my code looks like
CD_Drive = ???????
SourceFile = CD_Drive + "\folder\MyFile.doc"

Regards!

Michel




  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,452
Default Identify CD-Rom drive

This won't work in all situations (D could be a hard drive), but it is
simple and you
could expand on it, like looping through all the possible file paths.

Function FindFirstDriveAfterC() As String

Dim i As Byte

For i = 68 To 90
If Len(Dir(Chr(i) & ":\", vbDirectory)) 0 Then
FindFirstDriveAfterC = Chr(i)
Exit Function
End If
Next

End Function


Sub test()
MsgBox FindFirstDriveAfterC()
End Sub


RBS

"MD" wrote in message
. ..
Using FileCopy, I'd like to copy a file from a CD-Rom that is always in

the
same folder to the hard drive.

Sub TranferFiles()
SourceFile = "E:\folder\MyFile.doc"
DestinationFile = "C:\Temp\MyFile.doc"
FileCopy SourceFile, DestinationFile
End Sub

One problem with this....The CD-Rom drive is not always E from one PC to

the
other. So how do I identify the CD-Rom drive so that my code looks like
CD_Drive = ???????
SourceFile = CD_Drive + "\folder\MyFile.doc"

Regards!

Michel



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,452
Default Identify CD-Rom drive

If for some reason you want to keep it simple this might be an option:
It is based on the fact that you can't write to a CD-ROM.

Function FindFirstReadOnlyDriveAfterC() As String

Dim i As Byte

On Error GoTo ERROROUT

For i = 68 To 90
StringToTextFile Chr(i) & ":\zxzx.txt", "test"
Kill Chr(i) & ":\zxzx.txt"
Next

ERROROUT:

FindFirstReadOnlyDriveAfterC = Chr(i)

End Function


Sub StringToTextFile(ByVal txtFile As String, _
ByVal strString As String)

Dim hFile As Long

hFile = FreeFile

Open txtFile For Output As hFile
Write #hFile, strString
Close #hFile

End Sub


Sub test()
MsgBox FindFirstReadOnlyDriveAfterC()
End Sub


RBS


"MD" wrote in message
. ..
Using FileCopy, I'd like to copy a file from a CD-Rom that is always in

the
same folder to the hard drive.

Sub TranferFiles()
SourceFile = "E:\folder\MyFile.doc"
DestinationFile = "C:\Temp\MyFile.doc"
FileCopy SourceFile, DestinationFile
End Sub

One problem with this....The CD-Rom drive is not always E from one PC to

the
other. So how do I identify the CD-Rom drive so that my code looks like
CD_Drive = ???????
SourceFile = CD_Drive + "\folder\MyFile.doc"

Regards!

Michel





  #6   Report Post  
Posted to microsoft.public.excel.programming
MD MD is offline
external usenet poster
 
Posts: 26
Default Identify CD-Rom drive

Thank you all for you help

Michel


"Bob Phillips" a écrit dans le message
de ...
Michel,

Here is some code that I dapted from my CD-Player that will do it for you.
Got this from Randy Birch originally.

Public Declare Function GetLogicalDriveStrings Lib "kernel32" _
Alias "GetLogicalDriveStringsA" _
(ByVal nBufferLength As Long, _
ByVal lpBuffer As String) As Long

Public Declare Function GetDriveType Lib "kernel32" _
Alias "GetDriveTypeA" _
(ByVal nDrive As String) As Long

Public Const DRIVE_REMOVABLE = 2
Public Const DRIVE_FIXED = 3
Public Const DRIVE_REMOTE = 4
Public Const DRIVE_CDROM = 5
Public Const DRIVE_RAMDISK = 6


Private Sub GetCDDrive()
Dim r As Long
Dim sAll As String
Dim sDrive As String
Dim sType As String

'get the list of all available drives
sAll = GetDriveString()

'separate the drive strings and retrieve the drive type
Do Until sAll = Chr$(0)

sDrive = StripNulls(sAll)

Select Case GetDriveType(sDrive)
Case 0: sType = "The drive type cannot be determined"
Case 1: sType = "The root directory does not exist"

Case DRIVE_REMOVABLE:
Select Case Left$(sDrive, 1)
Case "a", "b": sType = "Floppy drive"
Case Else: sType = "Removable drive"
End Select

Case DRIVE_FIXED: sType = "Hard drive; can not be removed"
Case DRIVE_REMOTE: sType = "Remote (network) drive"
Case DRIVE_CDROM: sType = "CD-ROM drive"
Case DRIVE_RAMDISK: sType = "RAM disk"
End Select

If sType = "CD-ROM drive" Then
MsgBox "Drive " & sDrive & " is " & sType
End If

Loop

End Sub


Private Function GetDriveString() As String
Dim sBuffer As String

'possible 26 drives, three characters each, plus trailing null
sBuffer = Space$(26 * 4)

If GetLogicalDriveStrings(Len(sBuffer), sBuffer) Then
GetDriveString = Trim$(sBuffer)
End If

End Function

Private Function StripNulls(Start As String) As String
Dim iPos As Long

iPos = InStr(Start, Chr$(0))

If iPos Then
StripNulls = Mid$(Start, 1, iPos - 1)
Start = Mid$(Start, iPos + 1, Len(Start))
End If

End Function



--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

"MD" wrote in message
. ..
Using FileCopy, I'd like to copy a file from a CD-Rom that is always in

the
same folder to the hard drive.

Sub TranferFiles()
SourceFile = "E:\folder\MyFile.doc"
DestinationFile = "C:\Temp\MyFile.doc"
FileCopy SourceFile, DestinationFile
End Sub

One problem with this....The CD-Rom drive is not always E from one PC to

the
other. So how do I identify the CD-Rom drive so that my code looks like
CD_Drive = ???????
SourceFile = CD_Drive + "\folder\MyFile.doc"

Regards!

Michel






Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Links to mapped drive change to refer to local hard drive SueD Links and Linking in Excel 1 May 8th 08 11:42 AM
Obtain drive letter assignment of CD/DVD drive? EagleOne Excel Discussion (Misc queries) 1 October 13th 06 01:27 PM
Can I save to hard drive AND my flash drive at the same time? Gizelle Excel Discussion (Misc queries) 3 July 24th 06 08:27 PM
Pasting a range of information from a foler on F Drive to another folder on same drive Tom Ogilvy Excel Programming 1 August 3rd 03 01:50 AM
Find links with code; change from G drive to C drive Sandy[_3_] Excel Programming 4 July 26th 03 07:24 PM


All times are GMT +1. The time now is 09:22 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"