Link to home
Start Free TrialLog in
Avatar of BWATERS
BWATERS

asked on

Paths containing spaces

When using the mciSendString with a pathing variable, it doesn't like the spaces in the path.  For example:
MyPath = "c:\Program Files\filename.avi" confuses the command; whereas MyPath = "c:\Program_Files\filename.avi" is okay.  Any way around this?  I cannot alway control the directory name the user creates.
Avatar of obregoru
obregoru

Try """c:\Program Files\filename.avi"""

You need to include quotes in the string, and by placing two quotes together, you tell VB to use a quote as a character and not a delimiter.
Avatar of BWATERS

ASKER

Thanks for the reply...but NG. Here's what I have:

CmdStr = ("open " & AVIFileName & " Type AVIVideo alias Animation parent " _
           & LTrim$(Str$(frmAVI.hwnd)) + " style " + LTrim$(Str$(WS_CHILD)))

Where AVIFileName = F:\tgsc\KeySpan\RD Project Tracker\search.avi

When I send the CmsStr the mciSendString sees the spaces in the path and expects a parameter.  What can one do?
Strip the spaces from the path string. This might cause problems for paths that are supposed to include spaces (i.e. C:\Program Files\Office97).


This function will remove all spaces from a string. Use it to remove spaces from OldPath using the statement.

NewPath = NoSpace(OldPath)

Private Function NoSpace(ByVal PathString) As String
Dim i As Integer
Dim FirstPart As String
Dim SecondPart As String

   PathString = Trim$(PathString) ' get rid of leading/trailing spaces

Do
   i = InStr(PathString, " ")  ' find the first space
   If i = 0 Then Exit Do ' no more spaces found
   FirstPart = Left$(PathString, i - 1)
   SecondPart = Right$(PathString, Len(PathString) - i)
   PathString = FirstPart & SecondPart
Loop
NoSpace = PathString
End Function



This second function only removes those spaces that are next to the backslash characters, and leaves all the others in place. A similar method could eliminate spaces next to the colon, or the period characters.

Private Function NoSpace(ByVal PathString) As String
Dim i As Integer
Dim FirstPart As String
Dim SecondPart As String

   PathString = Trim$(PathString) ' get rid of leading/trailing spaces

Do
   i = InStr(PathString, " \")  ' space before slash
   If i = 0 Then Exit Do ' no more spaces found
   FirstPart = Left$(PathString, i - 1)
   SecondPart = Right$(PathString, Len(PathString) - i)
   PathString = FirstPart & SecondPart
Loop

Do
   i = InStr(PathString, "\ ")  ' space after slash
   If i = 0 Then Exit Do ' no more spaces found
   FirstPart = Left$(PathString, i)
   SecondPart = Right$(PathString, Len(PathString) - i - 1)
   PathString = FirstPart & SecondPart
Loop
NoSpace = PathString
End Function


Finally, this function will replace any spaces in the path with the underscore character:


Private Function NoSpace(ByVal PathString) As String
Dim i As Integer
Dim char As String

   PathString = Trim$(PathString) ' get rid of leading/trailing spaces

For i = 1 to Len(PathString)
   char = Mid$(PathString, i, 1)
   If char = " " Then Mid$(PathString, i, 1) = "_"
Next i

NoSpace = PathString
End Function
hddp666: his sample include "program files" so why answer this question.
BWATERS:
Use the short filename of the file. Here is a sample of how to convert the long filename to a short filename

Private Declare Function GetShortPathName Lib "kernel32" Alias "GetShortPathNameA" (ByVal lpszLongPath As String, ByVal lpszShortPath As String, ByVal cchBuffer As Long) As Long
Private Sub Command1_Click()
    Dim str As String
    str = String$(260, 0)
    Call GetShortPathName("c:\my documents\uitnodiging.ppt", str, 255)
    MsgBox str
End Sub

Avatar of BWATERS

ASKER

Thanks Mirkwood
ASKER CERTIFIED SOLUTION
Avatar of Mirkwood
Mirkwood

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial