VBA
Discuss AutoCAD ActiveX and VBA (Visual Basic for Applications) questions here.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Using VBA FindFile to find filenames with spaces in it.

2 REPLIES 2
Reply
Message 1 of 3
Anonymous
1889 Views, 2 Replies

Using VBA FindFile to find filenames with spaces in it.

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
2 REPLIES 2
Message 2 of 3
Anonymous
in reply to: Anonymous

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
>
>
Message 3 of 3
Anonymous
in reply to: Anonymous

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
> >
> >
>
>

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost