Discussion:
How do I get back path names like "C:\progra~1\visual~1\filename.doc"
(too old to reply)
David Ford
2004-02-21 17:11:57 UTC
Permalink
I'm trying to use the mciSendString command, and I don't think it likes the
verbose directory and file naming conventions in Windows. How can I get the
condensed path name as in the subject line?

Thanks.
Jim Edgar
2004-02-21 17:19:55 UTC
Permalink
Post by David Ford
I'm trying to use the mciSendString command, and I don't think it likes the
verbose directory and file naming conventions in Windows. How can I get the
condensed path name as in the subject line?
Thanks.
David --

Here's what I use. Watch for word wrapping.

Private Declare Function GetShortPathName Lib "kernel32" _
Alias "GetShortPathNameA" (ByVal lpszLongPath As String, _
ByVal lpszShortPath As String, _
ByVal cchBuffer As Long) As Long

Public Function ShortPathName(ByVal strPath As String) As String
'--------------------------------------------------------------
' Procedure : ShortPathName
' DateTime : 6/3/01 09:51
' Author : Edgar
' Purpose : Returns a short path (DOS) name from a valid path.
'--------------------------------------------------------------
Const MAX_PATH = 255
Dim strBuff As String
Dim lngRet As Long
Dim strRet As String

On Error GoTo ShortPathName_Error

If ((DirExists(strPath)) Or (FileExists(strPath))) Then
strBuff = Space$(MAX_PATH)
lngRet = GetShortPathName(strPath, strBuff, Len(strBuff))
strRet = Left$(strBuff, lngRet)
End If

ShortPathName_Exit:
ShortPathName = strRet
Exit Function

ShortPathName_Error:
strRet = ""
Err.Clear
Resume ShortPathName_Exit
End Function

Jim Edgar
Jim Edgar
2004-02-21 17:27:50 UTC
Permalink
Oops, better include the helper functions 8^)

Public Function DirExists(ByVal strPathName As String) As Boolean
'--------------------------------------------------------------
' Procedure : DirExists
' DateTime : 5/21/01 15:01
' Author : Edgar
' Purpose : Checks for existence of a directory.
'--------------------------------------------------------------
On Error GoTo DirExists_Error

Dim bRet As Boolean

bRet = (GetAttr(strPathName) And vbDirectory) = vbDirectory

DirExists_Exit:
DirExists = bRet
Exit Function

DirExists_Error:
Err.Clear
bRet = False
Resume DirExists_Exit
End Function

Public Function FileExists(ByVal strPathName As String) As Boolean
'--------------------------------------------------------------
' Procedure : FileExists
' DateTime : 5/21/01 15:01
' Author : Edgar
' Purpose : Checks for existence of a file.
'--------------------------------------------------------------
On Error GoTo FileExists_Error

Dim bRet As Boolean

bRet = (GetAttr(strPathName) And vbDirectory) = 0

FileExists_Exit:
FileExists = bRet
Exit Function

FileExists_Error:
Err.Clear
bRet = False
Resume FileExists_Exit
End Function
Larry Serflaten
2004-02-21 18:34:45 UTC
Permalink
Post by Jim Edgar
Public Function DirExists(ByVal strPathName As String) As Boolean
On Error GoTo DirExists_Error
Dim bRet As Boolean
bRet = (GetAttr(strPathName) And vbDirectory) = vbDirectory
DirExists = bRet
Exit Function
Err.Clear
bRet = False
Resume DirExists_Exit
End Function
My 'personal' preference has always been to avoid adding unecessary code.
With such a simple routine do you even look for that, or do you prefer
to follow some standard 'template'? For example:


Public Function DirExists(ByVal PathName As String) As Boolean
On Error Resume Next
DirExists = CBool(GetAttr(PathName) And vbDirectory)
Err.Clear
End Function


Maybe not a big difference in this case, but when you add it all up
at the end of an application, it can be a sizeable difference, for no
additional benefits....

LFS
Jim Edgar
2004-02-21 19:48:03 UTC
Permalink
Post by Larry Serflaten
Post by Jim Edgar
Public Function DirExists(ByVal strPathName As String) As Boolean
On Error GoTo DirExists_Error
Dim bRet As Boolean
bRet = (GetAttr(strPathName) And vbDirectory) = vbDirectory
DirExists = bRet
Exit Function
Err.Clear
bRet = False
Resume DirExists_Exit
End Function
My 'personal' preference has always been to avoid adding unecessary code.
With such a simple routine do you even look for that, or do you prefer
Public Function DirExists(ByVal PathName As String) As Boolean
On Error Resume Next
DirExists = CBool(GetAttr(PathName) And vbDirectory)
Err.Clear
End Function
Maybe not a big difference in this case, but when you add it all up
at the end of an application, it can be a sizeable difference, for no
additional benefits....
LFS
I'm probably on the opposite end of Rick Rothstein on this one. I tend
to add lots of comments and spread the steps over more lines. When I
was learning C we were given the assignment to rewrite some of the
built-in string functions (strcmp, strcat, etc..). The instructor gave
extra credit to the person who could do it in the fewest number of
lines. I won the extra credit but six months later it took me a half
hour to figure out the code. Since then my code has become much
more verbose.

As far as using a template, yes I do. I modified the MZTools template
so I usually have an error trap that includes a call to an error log. You're
right, obviously, about the size of the application, but since I'm not a
professional programmer I need to have my code spelled out in
front of me.

Jim Edgar
Bob O`Bob
2004-02-21 23:01:03 UTC
Permalink
Post by Larry Serflaten
My 'personal' preference has always been to avoid adding unecessary code.
Err.Clear
End Function
huh?
or were you giving that as an example of _questionable_ practice?
I'm afraid I wasn't able to follow this thread closely.

I definitely would skip any Err.Clear which was immediately followed
by the end of the procedure.


Bob
--
looking for work again <http://obob.com/bob/resume/>
Larry Serflaten
2004-02-22 04:49:43 UTC
Permalink
Post by Bob O`Bob
Post by Jim Edgar
Err.Clear
End Function
huh?
or were you giving that as an example of _questionable_ practice?
I'm afraid I wasn't able to follow this thread closely.
I definitely would skip any Err.Clear which was immediately followed
by the end of the procedure.
Why is that? In VB5 the end of a procedure does not clear the error.
Setting a handler does, so any previous error is lost when that routine
sets its handler. Using Err.Clear stops any error values within the
routine from being passed back...

LFS
Randy Birch
2004-02-22 04:55:00 UTC
Permalink
Same in VB6.
--
Randy Birch
MVP Visual Basic
http://vbnet.mvps.org/
Please respond only to the newsgroups so all can benefit.


"Larry Serflaten" <***@usinternet.com> wrote in message news:OYoAE7P#***@TK2MSFTNGP11.phx.gbl...
: "Bob O`Bob" <***@yahoogroups.com> wrote
: Why is that? In VB5 the end of a procedure does not clear the error.
: Setting a handler does, so any previous error is lost when that routine
: sets its handler. Using Err.Clear stops any error values within the
: routine from being passed back...
Bob O`Bob
2004-02-22 05:07:12 UTC
Permalink
Post by Randy Birch
Same in VB6.
wow, even more wierd!
I tested in VB6 earlier today and got different results.
I tested in VB5 just now and got what Larry said.
I tested again in VB6 and now it doesn't clear.
Too bad I never saved the earlier project,
now I'll probably never know where *my* error was.


Bob
--
looking for work again <http://obob.com/bob/resume/>
Jim Carlock
2004-02-22 05:18:49 UTC
Permalink
Hmmm...maybe that's where my problem is. Thanks guys! Something
to look at now and I think you provided an answer to a problem I'm
currently experiencing! Amazing! How did you know?
--
Jim Carlock
http://www.microcosmotalk.com/
Post replies to the newsgroup.
Post by Randy Birch
Same in VB6.
wow, even more wierd!
I tested in VB6 earlier today and got different results.
I tested in VB5 just now and got what Larry said.
I tested again in VB6 and now it doesn't clear.
Too bad I never saved the earlier project,
now I'll probably never know where *my* error was.


Bob
--
looking for work again <http://obob.com/bob/resume/>
Randy Birch
2004-02-22 05:38:55 UTC
Permalink
In newsgroup posting, the 'P' in MVP occasionally stands for
'paranormalist'. <g>
--
Randy Birch
MVP Visual Basic
http://vbnet.mvps.org/
Please respond only to the newsgroups so all can benefit.


"Jim Carlock" <***@10.10.com> wrote in message news:OKWgcNQ#***@TK2MSFTNGP11.phx.gbl...
: Hmmm...maybe that's where my problem is. Thanks guys! Something
: to look at now and I think you provided an answer to a problem I'm
: currently experiencing! Amazing! How did you know?
Rick Rothstein
2004-02-22 06:09:32 UTC
Permalink
Post by Bob O`Bob
wow, even more wierd!
I tested in VB6 earlier today and got different results.
I tested in VB5 just now and got what Larry said.
I tested again in VB6 and now it doesn't clear.
Too bad I never saved the earlier project,
now I'll probably never know where *my* error was.
Perhaps you used one of the listed items in this section taken from the help
files for Err Object?

The Err object's properties are reset to zero or zero-length strings ("")
after an Exit Sub, Exit Function, Exit Property or Resume Next statement
within an error-handling routine.

Rick - MVP
Randy Birch
2004-02-22 06:27:24 UTC
Permalink
Yes ... a valid err handler nukes the number automatically ...

Option Explicit

Private Sub somesub()

Dim x As Long
On Error Resume Next
x = 1 \ 0
Debug.Print Err.Number

End Sub

Private Sub somesubwithhandler()

Dim x As Long
On Error GoTo somesub_error
x = 1 \ 0

somesub_exit:
Exit Sub
somesub_error:
Debug.Print Err.Number
Resume Next

End Sub


Private Sub Command1_Click()

Debug.Print "somesub"
Call somesub
Debug.Print Err.Number

Debug.Print "somesubwithhandler"
Call somesubwithhandler
Debug.Print Err.Number

End Sub
--
Randy Birch
MVP Visual Basic
http://vbnet.mvps.org/
Please respond only to the newsgroups so all can benefit.


"Rick Rothstein" <***@NOSPAMcomcast.net> wrote in message news:#SbdwpQ#***@TK2MSFTNGP11.phx.gbl...
: > wow, even more wierd!
: > I tested in VB6 earlier today and got different results.
: > I tested in VB5 just now and got what Larry said.
: > I tested again in VB6 and now it doesn't clear.
: > Too bad I never saved the earlier project,
: > now I'll probably never know where *my* error was.
:
: Perhaps you used one of the listed items in this section taken from the
help
: files for Err Object?
:
: The Err object's properties are reset to zero or zero-length strings ("")
: after an Exit Sub, Exit Function, Exit Property or Resume Next statement
: within an error-handling routine.
:
: Rick - MVP
:
:
Bonj
2004-02-22 14:49:30 UTC
Permalink
Ar eyou just speculating that?
Post by Randy Birch
Same in VB6.
--
Randy Birch
MVP Visual Basic
http://vbnet.mvps.org/
Please respond only to the newsgroups so all can benefit.
: Why is that? In VB5 the end of a procedure does not clear the error.
: Setting a handler does, so any previous error is lost when that routine
: sets its handler. Using Err.Clear stops any error values within the
: routine from being passed back...
Randy Birch
2004-02-22 14:54:11 UTC
Permalink
I rarely speculate.
--
Randy Birch
MVP Visual Basic
http://vbnet.mvps.org/
Please respond only to the newsgroups so all can benefit.


"Bonj" <***@b.com> wrote in message news:OgI7PMV#***@TK2MSFTNGP12.phx.gbl...
: Ar eyou just speculating that?
Bob O`Bob
2004-02-22 05:04:40 UTC
Permalink
Post by Larry Serflaten
Why is that? In VB5 the end of a procedure does not clear the error.
Fascinating.
I hadn't been aware that was a change in VB6.
But I see from my own empirical testing that it is.


Bob
--
looking for work again <http://obob.com/bob/resume/>
David Ford
2004-02-21 19:02:50 UTC
Permalink
Post by Jim Edgar
David --
Here's what I use. Watch for word wrapping.
Private Declare Function GetShortPathName Lib "kernel32" _
Alias "GetShortPathNameA" (ByVal lpszLongPath As String, _
ByVal lpszShortPath As String, _
ByVal cchBuffer As Long) As Long
Great, thanks.
Rick Rothstein
2004-02-21 18:46:21 UTC
Permalink
Post by David Ford
I'm trying to use the mciSendString command, and I don't think it likes the
verbose directory and file naming conventions in Windows. How can I get the
condensed path name as in the subject line?
I've not played around with mciSendString myself; but whenever a command
balks at a long file/path name, usually passing it that name encased in
quotemarks helps. If your path/filename is stored in a variable named, say,
PathFileName, then try passing that part of the parameter this way

"""" & PathFileName & """"

or this way

Chr$(34) & PathFileName & Chr$(34)

whichever suits your taste. It might not work, but I'd give it a shot.

Rick - MVP
Loading...