lisp findfile equivelent in VBA

lisp findfile equivelent in VBA

mdhutchinson
Advisor Advisor
572 Views
3 Replies
Message 1 of 4

lisp findfile equivelent in VBA

mdhutchinson
Advisor
Advisor
The following works ...

Dim strMyFile as string
strMyFile = "C:\MyFile.txt"

If Not (Dir(strMyFile) Like "") then
…. do something
else
… do something else
end if

... However, isn't there a way more straightforward or obvious as what was intented when I come back to this later?
0 Likes
573 Views
3 Replies
Replies (3)
Message 2 of 4

Anonymous
Not applicable
[code]
Function oFile(sFile As String) As Object
Dim fs As Object
Set fs = CreateObject("Scripting.FileSystemObject")
Set File = fs.GetFile(sFile)
Set fs = Nothing
End Function

Function oFile2(sFile As String) As File
'This one with early binding will require a reference
'to Microsoft Scripting Runtime
'But now you have intellisense
Dim fs As Scripting.FileSystemObject
Set fs = CreateObject("Scripting.FileSystemObject")
Set File = fs.GetFile(sFile)
Set fs = Nothing
End Function
[/code]
0 Likes
Message 3 of 4

Anonymous
Not applicable
Hi,

Two other solutions are:
This one courtesy of Randall Rath:

Question


What in your opinion is the best way to check to see if a file exists in a
folder


The Common Answer:

Private Function DoesFileExist(strFnamePath As String) As Boolean

strFnamePath = Dir(strFnamePath)

If strFnamePath = "" Then

DoesFileExist = False

Else

DoesFileExist = True

End If

End Function




The Secret Answer:

The API provides an UNDOCUMENTED method. Place this code In a standard
module:

Declare Function PathFileExists Lib "shlwapi" Alias _

"PathFileExistsA" (ByVal strFile As String) As Long


Public Function FileExists(strFile As String) As Boolean

FileExists = PathFileExists(strFile)

End Function

End of Randall's information:

The next lot I found on the web, but did not document the source:

Find File quickly


Public Const MAX_PATH = 400

Public Type FILETIME

dwLowDateTime As Long

dwHighDateTime As Long

End Type


Public Type WIN32_FIND_DATA

dwFileAttributes As Long

ftCreationTime As FILETIME

ftLastAccessTime As FILETIME

ftLastWriteTime As FILETIME

nFileSizeHigh As Long

nFileSizeLow As Long

dwReserved0 As Long

dwReserved1 As Long

cFileName As String * MAX_PATH

cAlternate As String * 14

End Type




Public Declare Function FindFirstFile _

Lib "kernel32" _

Alias "FindFirstFileA" ( _

ByVal lpFileName As String, _

lpFindFileData As WIN32_FIND_DATA) _

As Long


Function FileExists(spFileSpec As String) As Boolean

Dim lFind As Long

Dim sSpec As String

Dim lpFind As WIN32_FIND_DATA

lFind = FindFirstFile(sSpec, lpFind)

If FindFirstFile(spFileSpec, lpFind) > 0 Then FileExists = True

End Function ' FileExists

My test call to the above:


Sub testfindfile()

Dim sSpec As String

sSpec = "C:\Program Files\CADApps\Linzlink\Settings\Table
Schemas\x_targetpoint.xsd"

'lFind = FindFirstFile(sSpec, tInfo)

If FileExists(sSpec) Then Debug.Print sSpec


End Sub


--

Laurie Comerford
CADApps
www.cadapps.com.au
www.civil3Dtools.com



wrote in message news:5450855@discussion.autodesk.com...
[code]
Function oFile(sFile As String) As Object
Dim fs As Object
Set fs = CreateObject("Scripting.FileSystemObject")
Set File = fs.GetFile(sFile)
Set fs = Nothing
End Function

Function oFile2(sFile As String) As File
'This one with early binding will require a reference
'to Microsoft Scripting Runtime
'But now you have intellisense
Dim fs As Scripting.FileSystemObject
Set fs = CreateObject("Scripting.FileSystemObject")
Set File = fs.GetFile(sFile)
Set fs = Nothing
End Function
[/code]
0 Likes
Message 4 of 4

Anonymous
Not applicable
Or the FileExists method of the FileSystemObject.

Dim fs As Object
Dim strMyFile as string
strMyFile = "C:\MyFile.txt"
Set fs = CreateObject("Scripting.FileSystemObject")

If Not (fs.FileExists(strMyFile)) then
.. do something
else
. do something else
end if
Set fs = Nothing

--
James Allen
Malicoat-Winslow Engineers, P.C.
Columbia, MO


wrote in message news:5450855@discussion.autodesk.com...
[code]
Function oFile(sFile As String) As Object
Dim fs As Object
Set fs = CreateObject("Scripting.FileSystemObject")
Set File = fs.GetFile(sFile)
Set fs = Nothing
End Function

Function oFile2(sFile As String) As File
'This one with early binding will require a reference
'to Microsoft Scripting Runtime
'But now you have intellisense
Dim fs As Scripting.FileSystemObject
Set fs = CreateObject("Scripting.FileSystemObject")
Set File = fs.GetFile(sFile)
Set fs = Nothing
End Function
[/code]
0 Likes