Solved

Paths containing spaces

Posted on 1998-12-30
6
283 Views
Last Modified: 2010-05-03
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.
0
Comment
Question by:BWATERS
6 Comments
 
LVL 2

Expert Comment

by:obregoru
ID: 1453329
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.
0
 

Author Comment

by:BWATERS
ID: 1453330
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?
0
 
LVL 1

Expert Comment

by:hddp666
ID: 1453331
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
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 13

Expert Comment

by:Mirkwood
ID: 1453332
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

0
 

Author Comment

by:BWATERS
ID: 1453333
Thanks Mirkwood
0
 
LVL 13

Accepted Solution

by:
Mirkwood earned 100 total points
ID: 1453334
Thankx

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

0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

790 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question