PC Review


Reply
Thread Tools Rate Thread

How to check drive exist or not using FileSystemObject

 
 
moonhk
Guest
Posts: n/a
 
      28th Dec 2006
Dear Reader


How to check drive exist or not using FileSystemObject ?

Private Sub cmdSelectName_Click()
Dim myFileName As Variant
Dim FileObj As New FileSystemObject
Dim loDriver As String
Dim loFolder As String
Dim loFilename As String
On Error Resume Next
loDriver = VBA.Trim(VBA.Left(txtfilename.Value, 2))
'~~ Check Drive exist of not

'~~ Change Drive
VBA.ChDrive (loDriver)
loFilename = FileObj.GetFileName(txtfilename.Value) '~~ Return
file name
If VBA.InStr(1, txtfilename.Value, loFilename, vbTextCompare) > 0
Then
loFolder = VBA.Left(txtfilename, VBA.InStr(1,
txtfilename.Value, loFilename, vbTextCompare) - 2)
End If

If FileObj.FolderExists(loFolder) Then
VBA.ChDir (loFolder)
End If
On Error GoTo 0
myFileName = Application.GetOpenFilename(filefilter:="Prn Files,
*.PRN", _
Title:="Pick a File")

If myFileName = False Then
'~~ MsgBox "Ok, try later" '~~user select cancel
Exit Sub
Else
txtfilename.Value = myFileName
End If
End Sub

 
Reply With Quote
 
 
 
 
horkuang@horkuang.com
Guest
Posts: n/a
 
      28th Dec 2006
Just use VBA and error trapping
loDriver = Left(txtfilename.Value, 1)

On Error Resume Next
ChDrive loDriver

If Err.Number > 0 Then
MsgBox "Drive """ & loDriver & """ not available"
Exit Sub
Else
On Error GoTo 0
'Continue
End If

NickHK

moonhk wrote:
> Dear Reader
>
>
> How to check drive exist or not using FileSystemObject ?
>
> Private Sub cmdSelectName_Click()
> Dim myFileName As Variant
> Dim FileObj As New FileSystemObject
> Dim loDriver As String
> Dim loFolder As String
> Dim loFilename As String
> On Error Resume Next
> loDriver = VBA.Trim(VBA.Left(txtfilename.Value, 2))
> '~~ Check Drive exist of not
>
> '~~ Change Drive
> VBA.ChDrive (loDriver)
> loFilename = FileObj.GetFileName(txtfilename.Value) '~~ Return
> file name
> If VBA.InStr(1, txtfilename.Value, loFilename, vbTextCompare) > 0
> Then
> loFolder = VBA.Left(txtfilename, VBA.InStr(1,
> txtfilename.Value, loFilename, vbTextCompare) - 2)
> End If
>
> If FileObj.FolderExists(loFolder) Then
> VBA.ChDir (loFolder)
> End If
> On Error GoTo 0
> myFileName = Application.GetOpenFilename(filefilter:="Prn Files,
> *.PRN", _
> Title:="Pick a File")
>
> If myFileName = False Then
> '~~ MsgBox "Ok, try later" '~~user select cancel
> Exit Sub
> Else
> txtfilename.Value = myFileName
> End If
> End Sub


 
Reply With Quote
 
moonhk
Guest
Posts: n/a
 
      28th Dec 2006
Any other suggestion ?

(E-Mail Removed) wrote:
> Just use VBA and error trapping
> loDriver = Left(txtfilename.Value, 1)
>
> On Error Resume Next
> ChDrive loDriver
>
> If Err.Number > 0 Then
> MsgBox "Drive """ & loDriver & """ not available"
> Exit Sub
> Else
> On Error GoTo 0
> 'Continue
> End If
>
> NickHK
>
> moonhk wrote:
> > Dear Reader
> >
> >
> > How to check drive exist or not using FileSystemObject ?
> >
> > Private Sub cmdSelectName_Click()
> > Dim myFileName As Variant
> > Dim FileObj As New FileSystemObject
> > Dim loDriver As String
> > Dim loFolder As String
> > Dim loFilename As String
> > On Error Resume Next
> > loDriver = VBA.Trim(VBA.Left(txtfilename.Value, 2))
> > '~~ Check Drive exist of not
> >
> > '~~ Change Drive
> > VBA.ChDrive (loDriver)
> > loFilename = FileObj.GetFileName(txtfilename.Value) '~~ Return
> > file name
> > If VBA.InStr(1, txtfilename.Value, loFilename, vbTextCompare) > 0
> > Then
> > loFolder = VBA.Left(txtfilename, VBA.InStr(1,
> > txtfilename.Value, loFilename, vbTextCompare) - 2)
> > End If
> >
> > If FileObj.FolderExists(loFolder) Then
> > VBA.ChDir (loFolder)
> > End If
> > On Error GoTo 0
> > myFileName = Application.GetOpenFilename(filefilter:="Prn Files,
> > *.PRN", _
> > Title:="Pick a File")
> >
> > If myFileName = False Then
> > '~~ MsgBox "Ok, try later" '~~user select cancel
> > Exit Sub
> > Else
> > txtfilename.Value = myFileName
> > End If
> > End Sub


 
Reply With Quote
 
Jim Cone
Guest
Posts: n/a
 
      28th Dec 2006
From the Scripting Runtime v5.6 help file...

Function ReportDriveStatus(drv)
Dim fso, msg
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.DriveExists(drv) Then
msg = ("Drive " & UCase(drv) & " exists.")
Else
msg = ("Drive " & UCase(drv) & " doesn't exist.")
End If
ReportDriveStatus = msg
End Function
--
Jim Cone
San Francisco, USA
http://www.realezsites.com/bus/primitivesoftware


"moonhk" <moon_ils-(E-Mail Removed)>
wrote in message
Any other suggestion ?

 
Reply With Quote
 
Chip Pearson
Guest
Posts: n/a
 
      28th Dec 2006
You could do with a a variety of API calls, but the simplest way is to try
to ChDrive to the drive in question and test for an error:

Dim SaveDir As String
SaveDir = CurDir ' save CurDir to restore defaults
On Error Resume Next
ChDrive "Z:\" ' test for drive 'Z'
If Err.Number <> 0 Then
Debug.Print "Drive does not exist"
Else
Debug.Print "Drive exists."
End If
ChDrive SaveDir
ChDir SaveDir



--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
(email address is on the web site)

"moonhk" <moon_ils-(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Dear Reader
>
>
> How to check drive exist or not using FileSystemObject ?
>
> Private Sub cmdSelectName_Click()
> Dim myFileName As Variant
> Dim FileObj As New FileSystemObject
> Dim loDriver As String
> Dim loFolder As String
> Dim loFilename As String
> On Error Resume Next
> loDriver = VBA.Trim(VBA.Left(txtfilename.Value, 2))
> '~~ Check Drive exist of not
>
> '~~ Change Drive
> VBA.ChDrive (loDriver)
> loFilename = FileObj.GetFileName(txtfilename.Value) '~~ Return
> file name
> If VBA.InStr(1, txtfilename.Value, loFilename, vbTextCompare) > 0
> Then
> loFolder = VBA.Left(txtfilename, VBA.InStr(1,
> txtfilename.Value, loFilename, vbTextCompare) - 2)
> End If
>
> If FileObj.FolderExists(loFolder) Then
> VBA.ChDir (loFolder)
> End If
> On Error GoTo 0
> myFileName = Application.GetOpenFilename(filefilter:="Prn Files,
> *.PRN", _
> Title:="Pick a File")
>
> If myFileName = False Then
> '~~ MsgBox "Ok, try later" '~~user select cancel
> Exit Sub
> Else
> txtfilename.Value = myFileName
> End If
> End Sub
>



 
Reply With Quote
 
Dana DeLouis
Guest
Posts: n/a
 
      28th Dec 2006
> How to check drive exist or not using FileSystemObject ?

This idea uses the "DriveExists" method.

Function FileExistsQ(sFilePath As String) As Boolean
With CreateObject("Scripting.FileSystemObject")
FileExistsQ = .DriveExists(.GetDriveName(sFilePath))
End With
End Function

Sub TestIt()
Debug.Print FileExistsQ("C:\Windows")
Debug.Print FileExistsQ("A:\Windows")
Debug.Print FileExistsQ("D:\")
End Sub

Note the following when testing something like an A: Drive:
'A' might exist, but it is not "Ready" because it lacks a Disk.
You may want to include an "IsReady" to make sure it has a Disk.
--
HTH :>)
Dana DeLouis
Windows XP & Office 2003


"moonhk" <moon_ils-(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Dear Reader
>
>
> How to check drive exist or not using FileSystemObject ?
>
> Private Sub cmdSelectName_Click()
> Dim myFileName As Variant
> Dim FileObj As New FileSystemObject
> Dim loDriver As String
> Dim loFolder As String
> Dim loFilename As String
> On Error Resume Next
> loDriver = VBA.Trim(VBA.Left(txtfilename.Value, 2))
> '~~ Check Drive exist of not
>
> '~~ Change Drive
> VBA.ChDrive (loDriver)
> loFilename = FileObj.GetFileName(txtfilename.Value) '~~ Return
> file name
> If VBA.InStr(1, txtfilename.Value, loFilename, vbTextCompare) > 0
> Then
> loFolder = VBA.Left(txtfilename, VBA.InStr(1,
> txtfilename.Value, loFilename, vbTextCompare) - 2)
> End If
>
> If FileObj.FolderExists(loFolder) Then
> VBA.ChDir (loFolder)
> End If
> On Error GoTo 0
> myFileName = Application.GetOpenFilename(filefilter:="Prn Files,
> *.PRN", _
> Title:="Pick a File")
>
> If myFileName = False Then
> '~~ MsgBox "Ok, try later" '~~user select cancel
> Exit Sub
> Else
> txtfilename.Value = myFileName
> End If
> End Sub
>



 
Reply With Quote
 
Dana DeLouis
Guest
Posts: n/a
 
      28th Dec 2006
>> How to check drive exist ...

Ahhh! I'd like to change the name of that function.

Function DriveExistsQ(sFilePath As String) As Boolean
With CreateObject("Scripting.FileSystemObject")
DriveExistsQ = .DriveExists(.GetDriveName(sFilePath))
End With
End Function

--
Dana DeLouis
Windows XP & Office 2003


"Dana DeLouis" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>> How to check drive exist or not using FileSystemObject ?

>
> This idea uses the "DriveExists" method.
>
> Function FileExistsQ(sFilePath As String) As Boolean
> With CreateObject("Scripting.FileSystemObject")
> FileExistsQ = .DriveExists(.GetDriveName(sFilePath))
> End With
> End Function
>
> Sub TestIt()
> Debug.Print FileExistsQ("C:\Windows")
> Debug.Print FileExistsQ("A:\Windows")
> Debug.Print FileExistsQ("D:\")
> End Sub
>
> Note the following when testing something like an A: Drive:
> 'A' might exist, but it is not "Ready" because it lacks a Disk.
> You may want to include an "IsReady" to make sure it has a Disk.
> --
> HTH :>)
> Dana DeLouis
> Windows XP & Office 2003
>
>
> "moonhk" <moon_ils-(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> Dear Reader
>>
>>
>> How to check drive exist or not using FileSystemObject ?
>>
>> Private Sub cmdSelectName_Click()
>> Dim myFileName As Variant
>> Dim FileObj As New FileSystemObject
>> Dim loDriver As String
>> Dim loFolder As String
>> Dim loFilename As String
>> On Error Resume Next
>> loDriver = VBA.Trim(VBA.Left(txtfilename.Value, 2))
>> '~~ Check Drive exist of not
>>
>> '~~ Change Drive
>> VBA.ChDrive (loDriver)
>> loFilename = FileObj.GetFileName(txtfilename.Value) '~~ Return
>> file name
>> If VBA.InStr(1, txtfilename.Value, loFilename, vbTextCompare) > 0
>> Then
>> loFolder = VBA.Left(txtfilename, VBA.InStr(1,
>> txtfilename.Value, loFilename, vbTextCompare) - 2)
>> End If
>>
>> If FileObj.FolderExists(loFolder) Then
>> VBA.ChDir (loFolder)
>> End If
>> On Error GoTo 0
>> myFileName = Application.GetOpenFilename(filefilter:="Prn Files,
>> *.PRN", _
>> Title:="Pick a File")
>>
>> If myFileName = False Then
>> '~~ MsgBox "Ok, try later" '~~user select cancel
>> Exit Sub
>> Else
>> txtfilename.Value = myFileName
>> End If
>> End Sub
>>

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Check for a tab if it is exist Farhad Microsoft Excel Misc 2 21st Dec 2008 07:31 PM
FileSystemObject drive error =?Utf-8?B?NDExMA==?= Microsoft Access VBA Modules 4 21st Mar 2007 11:15 AM
Re: Check table exist in ADO David Lloyd Microsoft Access VBA Modules 0 8th Jul 2005 03:05 PM
check SQL db exist... =?Utf-8?B?Sm9u?= Microsoft VB .NET 3 17th May 2005 12:57 PM
Check if value exist Daniel Microsoft Access Form Coding 2 14th Oct 2003 10:53 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:51 PM.