Visual Basic Customization

Visual Basic Customization

Reply
*Cochran, Doug
Message 1 of 3 (691 Views)

Using VBA FindFile to find filenames with spaces in it.

691 Views, 2 Replies
01-03-2003 12:37 PM
I'm trying to use this code (by Mr. Collin French circa 1999/2000) to search
the autocad support paths to find autocad files. It works fine if the file
name doesn't have a space in it. Does anyone have any ideas on how to have
it find files with spaces in them also? Or another method/code to find files
in VB/VBA? I've tried using Chr$(34) to add "quotes" to the filename but
that still fails.

thanks in advance,

Doug Cochran


3. A FindFile() function for VBA applications
---------------------------------------------

This code has been provided by Mr Collin French on the
news://adesknews.autodesk.com/autodesk.autocad.customization.vba
newsgroup.

The code shown here has been modified in format only
to fit within the confines of the newsletter format.


'----FindFile----------------------------------------
' This function looks for the specified filename in
' the current directory, the current drawing's
' directory, any of AutoCAD's support paths, and the
' directory ACAD is running from. If it finds
' the file, it will return the full path+filename.
' If not, it will return an empty string. If the
' specified filename includes any backslashes ( \ ),
' it is assumed that it's a full path+filename
' which must be checked to see if it exists, rather
' than searching amongst AutoCAD's paths. This
' function does not really work with wildcards. If it
' finds a file which matches the filespec, it
' will return the path plus the original filespec,
' eg. C:\*.bat, rather than C:\Autoexec.bat .
'-----------------------------------------------------
Public Function FindFile(FileName As String) As String
Dim Path As String 'holds path to check
Dim SupportPath As String 'copy of ACAD's support paths
Dim Pos As Integer 'position within SupportPath of separator
'----if supplied name has embedded '\' assume it is full path+filename
If InStr(1, FileName, "\") > 0 Then 'look for embedded backslash
FindFile = FileName 'copy supplied path+filename for result
On Error Resume Next 'in case of disk error
'if doesn't exist, change result to ""
If Dir$(FileName) = "" Then FindFile = ""
If Err Then FindFile = "" 'if some file error, indicate failure
Exit Function 'return result to calling routine
End If
'----check in current directory---------------------
'construct path+filename to check
FindFile = CurDir$ & "\" & FileName
'if exists, return it as the result
If Dir$(FindFile) <> "" Then Exit Function
'----check in drawing's directory-------------------
'in ACAD 14.01, same as ThisDrawing.Path
Path = ThisDrawing.FullName
'ensure not a new, unnamed drawing
If Path <> "" Then
'chop drawing name from end of 'path'
Path = Left$(Path, Len(Path) - Len(ThisDrawing.Name))
'construct path+filename
FindFile = Path & FileName
'if exists, return it as the result
If Dir$(FindFile) <> "" Then Exit Function
End If
'----check ACAD's support directories----------------
'copy of support paths
SupportPath = ThisDrawing.Application.Preferences.SupportPath
'ensure has trailing '\'
If Right$(SupportPath, 1) <> "\" Then SupportPath = SupportPath & "\"
'check each one until none left
Do While Len(SupportPath) > 0
'look for separator between paths
Pos = InStr(1, SupportPath, ";")
'if found a separator character...
If Pos > 0 Then
'...extract path before it
Path = Left$(SupportPath, Pos - 1)
'no separator, so...
Else
'...must be last path in SupportPath
Path = SupportPath
End If
'make sure has trailing backslash
If Right$(Path, 1) <> "\" Then Path = Path & "\"
'construct path+filename to check
FindFile = Path & FileName
'if exists, return as result
If Dir$(FindFile) <> "" Then Exit Function
'chop off path just checked
SupportPath = Right$(SupportPath, Len(SupportPath) - Len(Path))
'probably separator still there
If Left$(SupportPath, 1) = ";" Then
'chop it off too
SupportPath = Right$(SupportPath, Len(SupportPath) - 1)
End If
'check next path in SupportPath
Loop
'----check in AutoCAD's program directory-------------
'get path to ACAD.EXE
Path = ThisDrawing.Application.Path
'make sure has trailing backslash
If Right$(Path, 1) <> "\" Then Path = Path & "\"
'construct path+filename to check
FindFile = Path & FileName
'if exists, return as result
If Dir$(FindFile) <> "" Then Exit Function
'----can't find file, return empty string to indicate failure
FindFile = ""
End Function

'----TestFindFile----------------------------------------
' This routine is used to test the FindFile function.
' It brings up an InputBox so the user can
' enter the name of a file to check for, calls FindFile(),
' then displays the result in a MsgBox.
'--------------------------------------------------------
Public Sub TestFindFile()
Dim res As String 'holds the result of function calls
Dim msg As String messages to present to user
'----prompt user for filename to test
msg = "Enter the filename you want to try with the FindFile function." &
vbCrLf & vbCrLf
msg = msg & "FindFile will look in the current directory, the directory" &
vbCrLf
msg = msg & "containing the current drawing (if any), AutoCAD's support" &
vbCrLf
msg = msg & "directories, and the directory AutoCAD is running from." &
vbCrLf
res = InputBox(msg, "Testing FindFile", "acad14.cfg")
'----use the FindFile function retrieve full path+filename
'if user pressed 'Cancel', res = ""
If res <> "" Then
'search for file
res = FindFile(res)
'FindFile returns "" if can't find file
If res = "" Then
'compose message to that effect and
msg = "Couldn't find that file."
'tell user what happened
MsgBox msg, vbInformation, "Testing FindFile"
Else
'FindFile returned full path+filename
msg = "Found that file in: '" & res & "'."
'show result to user
MsgBox msg, vbInformation, "Testing FindFile"
End If
End If
End Sub
*Obando, Humberto Guerrero
Message 2 of 3 (691 Views)

Re: Using VBA FindFile to find filenames with spaces in it.

01-03-2003 01:52 PM in reply to: *Cochran, Doug
Public Declare Function SearchPath Lib "kernel32" Alias "SearchPathA" _
(ByVal lpPath As String, _
ByVal lpFileName As String, _
ByVal lpExtension As String, _
ByVal nBufferLength As Long, _
ByVal lpBuffer As String, _
ByVal lpFilePart As String) As Long

Public Const MAX_PATH = 260

Public Function FindFile(sFileName As String, Optional sPath As String =
vbNullString) As String

Dim retVal As Long, lpBuffer As String * MAX_PATH

retVal = SearchPath(sPath, sFileName, vbNullString, MAX_PATH, lpBuffer,
vbNullString)
FindFile = IIf(retVal, Left(lpBuffer, retVal), "ERROR")

End Function



Example
FindFile("autoexec.bat")

this code is from http://code.acadx.com/


"Doug Cochran" wrote in message
news:B36C85FB1F3CF1505FEFEDFBDF5719CA@in.WebX.maYIadrTaRb...
> I'm trying to use this code (by Mr. Collin French circa 1999/2000) to
search
> the autocad support paths to find autocad files. It works fine if the
file
> name doesn't have a space in it. Does anyone have any ideas on how to
have
> it find files with spaces in them also? Or another method/code to find
files
> in VB/VBA? I've tried using Chr$(34) to add "quotes" to the filename but
> that still fails.
>
> thanks in advance,
>
> Doug Cochran
>
>
> 3. A FindFile() function for VBA applications
> ---------------------------------------------
>
> This code has been provided by Mr Collin French on the
> news://adesknews.autodesk.com/autodesk.autocad.customization.vba
> newsgroup.
>
> The code shown here has been modified in format only
> to fit within the confines of the newsletter format.
>
>
> '----FindFile----------------------------------------
> ' This function looks for the specified filename in
> ' the current directory, the current drawing's
> ' directory, any of AutoCAD's support paths, and the
> ' directory ACAD is running from. If it finds
> ' the file, it will return the full path+filename.
> ' If not, it will return an empty string. If the
> ' specified filename includes any backslashes ( \ ),
> ' it is assumed that it's a full path+filename
> ' which must be checked to see if it exists, rather
> ' than searching amongst AutoCAD's paths. This
> ' function does not really work with wildcards. If it
> ' finds a file which matches the filespec, it
> ' will return the path plus the original filespec,
> ' eg. C:\*.bat, rather than C:\Autoexec.bat .
> '-----------------------------------------------------
> Public Function FindFile(FileName As String) As String
> Dim Path As String 'holds path to check
> Dim SupportPath As String 'copy of ACAD's support paths
> Dim Pos As Integer 'position within SupportPath of separator
> '----if supplied name has embedded '\' assume it is full path+filename
> If InStr(1, FileName, "\") > 0 Then 'look for embedded backslash
> FindFile = FileName 'copy supplied path+filename for result
> On Error Resume Next 'in case of disk error
> 'if doesn't exist, change result to ""
> If Dir$(FileName) = "" Then FindFile = ""
> If Err Then FindFile = "" 'if some file error, indicate failure
> Exit Function 'return result to calling routine
> End If
> '----check in current directory---------------------
> 'construct path+filename to check
> FindFile = CurDir$ & "\" & FileName
> 'if exists, return it as the result
> If Dir$(FindFile) <> "" Then Exit Function
> '----check in drawing's directory-------------------
> 'in ACAD 14.01, same as ThisDrawing.Path
> Path = ThisDrawing.FullName
> 'ensure not a new, unnamed drawing
> If Path <> "" Then
> 'chop drawing name from end of 'path'
> Path = Left$(Path, Len(Path) - Len(ThisDrawing.Name))
> 'construct path+filename
> FindFile = Path & FileName
> 'if exists, return it as the result
> If Dir$(FindFile) <> "" Then Exit Function
> End If
> '----check ACAD's support directories----------------
> 'copy of support paths
> SupportPath = ThisDrawing.Application.Preferences.SupportPath
> 'ensure has trailing '\'
> If Right$(SupportPath, 1) <> "\" Then SupportPath = SupportPath & "\"
> 'check each one until none left
> Do While Len(SupportPath) > 0
> 'look for separator between paths
> Pos = InStr(1, SupportPath, ";")
> 'if found a separator character...
> If Pos > 0 Then
> '...extract path before it
> Path = Left$(SupportPath, Pos - 1)
> 'no separator, so...
> Else
> '...must be last path in SupportPath
> Path = SupportPath
> End If
> 'make sure has trailing backslash
> If Right$(Path, 1) <> "\" Then Path = Path & "\"
> 'construct path+filename to check
> FindFile = Path & FileName
> 'if exists, return as result
> If Dir$(FindFile) <> "" Then Exit Function
> 'chop off path just checked
> SupportPath = Right$(SupportPath, Len(SupportPath) - Len(Path))
> 'probably separator still there
> If Left$(SupportPath, 1) = ";" Then
> 'chop it off too
> SupportPath = Right$(SupportPath, Len(SupportPath) - 1)
> End If
> 'check next path in SupportPath
> Loop
> '----check in AutoCAD's program directory-------------
> 'get path to ACAD.EXE
> Path = ThisDrawing.Application.Path
> 'make sure has trailing backslash
> If Right$(Path, 1) <> "\" Then Path = Path & "\"
> 'construct path+filename to check
> FindFile = Path & FileName
> 'if exists, return as result
> If Dir$(FindFile) <> "" Then Exit Function
> '----can't find file, return empty string to indicate failure
> FindFile = ""
> End Function
>
> '----TestFindFile----------------------------------------
> ' This routine is used to test the FindFile function.
> ' It brings up an InputBox so the user can
> ' enter the name of a file to check for, calls FindFile(),
> ' then displays the result in a MsgBox.
> '--------------------------------------------------------
> Public Sub TestFindFile()
> Dim res As String 'holds the result of function calls
> Dim msg As String messages to present to user
> '----prompt user for filename to test
> msg = "Enter the filename you want to try with the FindFile function." &
> vbCrLf & vbCrLf
> msg = msg & "FindFile will look in the current directory, the directory"
&
> vbCrLf
> msg = msg & "containing the current drawing (if any), AutoCAD's support"
&
> vbCrLf
> msg = msg & "directories, and the directory AutoCAD is running from." &
> vbCrLf
> res = InputBox(msg, "Testing FindFile", "acad14.cfg")
> '----use the FindFile function retrieve full path+filename
> 'if user pressed 'Cancel', res = ""
> If res <> "" Then
> 'search for file
> res = FindFile(res)
> 'FindFile returns "" if can't find file
> If res = "" Then
> 'compose message to that effect and
> msg = "Couldn't find that file."
> 'tell user what happened
> MsgBox msg, vbInformation, "Testing FindFile"
> Else
> 'FindFile returned full path+filename
> msg = "Found that file in: '" & res & "'."
> 'show result to user
> MsgBox msg, vbInformation, "Testing FindFile"
> End If
> End If
> End Sub
>
>
*Cochran, Doug
Message 3 of 3 (691 Views)

Re:

01-03-2003 04:19 PM in reply to: *Cochran, Doug
Short, Sweet and it worked!!! Thanks!


"Humberto Guerrero Obando" wrote in message
news:1470C28CF021D1632A0AAC0AF73390F6@in.WebX.maYIadrTaRb...
> Public Declare Function SearchPath Lib "kernel32" Alias "SearchPathA" _
> (ByVal lpPath As String, _
> ByVal lpFileName As String, _
> ByVal lpExtension As String, _
> ByVal nBufferLength As Long, _
> ByVal lpBuffer As String, _
> ByVal lpFilePart As String) As Long
>
> Public Const MAX_PATH = 260
>
> Public Function FindFile(sFileName As String, Optional sPath As String =
> vbNullString) As String
>
> Dim retVal As Long, lpBuffer As String * MAX_PATH
>
> retVal = SearchPath(sPath, sFileName, vbNullString, MAX_PATH,
lpBuffer,
> vbNullString)
> FindFile = IIf(retVal, Left(lpBuffer, retVal), "ERROR")
>
> End Function
>
>
>
> Example
> FindFile("autoexec.bat")
>
> this code is from http://code.acadx.com/
>
>
> "Doug Cochran" wrote in message
> news:B36C85FB1F3CF1505FEFEDFBDF5719CA@in.WebX.maYIadrTaRb...
> > I'm trying to use this code (by Mr. Collin French circa 1999/2000) to
> search
> > the autocad support paths to find autocad files. It works fine if the
> file
> > name doesn't have a space in it. Does anyone have any ideas on how to
> have
> > it find files with spaces in them also? Or another method/code to find
> files
> > in VB/VBA? I've tried using Chr$(34) to add "quotes" to the filename
but
> > that still fails.
> >
> > thanks in advance,
> >
> > Doug Cochran
> >
> >
> > 3. A FindFile() function for VBA applications
> > ---------------------------------------------
> >
> > This code has been provided by Mr Collin French on the
> > news://adesknews.autodesk.com/autodesk.autocad.customization.vba
> > newsgroup.
> >
> > The code shown here has been modified in format only
> > to fit within the confines of the newsletter format.
> >
> >
> > '----FindFile----------------------------------------
> > ' This function looks for the specified filename in
> > ' the current directory, the current drawing's
> > ' directory, any of AutoCAD's support paths, and the
> > ' directory ACAD is running from. If it finds
> > ' the file, it will return the full path+filename.
> > ' If not, it will return an empty string. If the
> > ' specified filename includes any backslashes ( \ ),
> > ' it is assumed that it's a full path+filename
> > ' which must be checked to see if it exists, rather
> > ' than searching amongst AutoCAD's paths. This
> > ' function does not really work with wildcards. If it
> > ' finds a file which matches the filespec, it
> > ' will return the path plus the original filespec,
> > ' eg. C:\*.bat, rather than C:\Autoexec.bat .
> > '-----------------------------------------------------
> > Public Function FindFile(FileName As String) As String
> > Dim Path As String 'holds path to check
> > Dim SupportPath As String 'copy of ACAD's support paths
> > Dim Pos As Integer 'position within SupportPath of separator
> > '----if supplied name has embedded '\' assume it is full path+filename
> > If InStr(1, FileName, "\") > 0 Then 'look for embedded backslash
> > FindFile = FileName 'copy supplied path+filename for result
> > On Error Resume Next 'in case of disk error
> > 'if doesn't exist, change result to ""
> > If Dir$(FileName) = "" Then FindFile = ""
> > If Err Then FindFile = "" 'if some file error, indicate failure
> > Exit Function 'return result to calling routine
> > End If
> > '----check in current directory---------------------
> > 'construct path+filename to check
> > FindFile = CurDir$ & "\" & FileName
> > 'if exists, return it as the result
> > If Dir$(FindFile) <> "" Then Exit Function
> > '----check in drawing's directory-------------------
> > 'in ACAD 14.01, same as ThisDrawing.Path
> > Path = ThisDrawing.FullName
> > 'ensure not a new, unnamed drawing
> > If Path <> "" Then
> > 'chop drawing name from end of 'path'
> > Path = Left$(Path, Len(Path) - Len(ThisDrawing.Name))
> > 'construct path+filename
> > FindFile = Path & FileName
> > 'if exists, return it as the result
> > If Dir$(FindFile) <> "" Then Exit Function
> > End If
> > '----check ACAD's support directories----------------
> > 'copy of support paths
> > SupportPath = ThisDrawing.Application.Preferences.SupportPath
> > 'ensure has trailing '\'
> > If Right$(SupportPath, 1) <> "\" Then SupportPath = SupportPath & "\"
> > 'check each one until none left
> > Do While Len(SupportPath) > 0
> > 'look for separator between paths
> > Pos = InStr(1, SupportPath, ";")
> > 'if found a separator character...
> > If Pos > 0 Then
> > '...extract path before it
> > Path = Left$(SupportPath, Pos - 1)
> > 'no separator, so...
> > Else
> > '...must be last path in SupportPath
> > Path = SupportPath
> > End If
> > 'make sure has trailing backslash
> > If Right$(Path, 1) <> "\" Then Path = Path & "\"
> > 'construct path+filename to check
> > FindFile = Path & FileName
> > 'if exists, return as result
> > If Dir$(FindFile) <> "" Then Exit Function
> > 'chop off path just checked
> > SupportPath = Right$(SupportPath, Len(SupportPath) - Len(Path))
> > 'probably separator still there
> > If Left$(SupportPath, 1) = ";" Then
> > 'chop it off too
> > SupportPath = Right$(SupportPath, Len(SupportPath) - 1)
> > End If
> > 'check next path in SupportPath
> > Loop
> > '----check in AutoCAD's program directory-------------
> > 'get path to ACAD.EXE
> > Path = ThisDrawing.Application.Path
> > 'make sure has trailing backslash
> > If Right$(Path, 1) <> "\" Then Path = Path & "\"
> > 'construct path+filename to check
> > FindFile = Path & FileName
> > 'if exists, return as result
> > If Dir$(FindFile) <> "" Then Exit Function
> > '----can't find file, return empty string to indicate failure
> > FindFile = ""
> > End Function
> >
> > '----TestFindFile----------------------------------------
> > ' This routine is used to test the FindFile function.
> > ' It brings up an InputBox so the user can
> > ' enter the name of a file to check for, calls FindFile(),
> > ' then displays the result in a MsgBox.
> > '--------------------------------------------------------
> > Public Sub TestFindFile()
> > Dim res As String 'holds the result of function calls
> > Dim msg As String messages to present to user
> > '----prompt user for filename to test
> > msg = "Enter the filename you want to try with the FindFile function."
&
> > vbCrLf & vbCrLf
> > msg = msg & "FindFile will look in the current directory, the
directory"
> &
> > vbCrLf
> > msg = msg & "containing the current drawing (if any), AutoCAD's
support"
> &
> > vbCrLf
> > msg = msg & "directories, and the directory AutoCAD is running from."
&
> > vbCrLf
> > res = InputBox(msg, "Testing FindFile", "acad14.cfg")
> > '----use the FindFile function retrieve full path+filename
> > 'if user pressed 'Cancel', res = ""
> > If res <> "" Then
> > 'search for file
> > res = FindFile(res)
> > 'FindFile returns "" if can't find file
> > If res = "" Then
> > 'compose message to that effect and
> > msg = "Couldn't find that file."
> > 'tell user what happened
> > MsgBox msg, vbInformation, "Testing FindFile"
> > Else
> > 'FindFile returned full path+filename
> > msg = "Found that file in: '" & res & "'."
> > 'show result to user
> > MsgBox msg, vbInformation, "Testing FindFile"
> > End If
> > End If
> > End Sub
> >
> >
>
>
Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Do you have 60 seconds to spare? The Autodesk Community Team is revamping our site ranking system and we want your feedback! Please click here to launch the 5 question survey. As always your input is greatly appreciated.